Skip to content

Commit 1b0535a

Browse files
authored
Merge branch 'main' into quiz-spread-syntax-correction
2 parents dfc944e + 86b341e commit 1b0535a

File tree

56 files changed

+904
-615
lines changed
  • questions
    • can-you-offer-a-use-case-for-the-new-arrow-function-syntax-how-does-this-new-syntax-differ-from-other-functions
    • difference-between-function-person-var-person-person-and-var-person-new-person
    • explain-hoisting
    • explain-how-prototypal-inheritance-works
    • explain-the-concept-of-a-callback-function-in-asynchronous-operations
    • explain-the-concept-of-destructuring-assignment-for-objects-and-arrays
    • explain-the-concept-of-hoisting-with-regards-to-functions
    • explain-the-concept-of-inheritance-in-es2015-classes
    • explain-the-concept-of-lexical-scoping
    • explain-the-concept-of-scope-in-javascript
    • explain-the-concept-of-the-spread-operator-and-its-uses
    • explain-the-difference-between-classical-inheritance-and-prototypal-inheritance
    • explain-the-difference-between-dot-notation-and-bracket-notation-for-accessing-object-properties
    • explain-the-difference-between-global-scope-function-scope-and-block-scope
    • explain-the-difference-between-shallow-copy-and-deep-copy
    • explain-the-difference-between-synchronous-and-asynchronous-functions
    • explain-the-difference-in-hoisting-between-var-let-and-const
    • explain-the-differences-on-the-usage-of-foo-between-function-foo-and-var-foo-function
    • how-can-closures-be-used-to-create-private-variables
    • how-do-you-access-the-index-of-an-element-in-an-array-during-iteration
    • how-do-you-add-remove-and-update-elements-in-an-array
    • how-do-you-check-if-an-object-has-a-specific-property
    • how-do-you-check-the-data-type-of-a-variable
    • how-do-you-convert-a-string-to-a-number-in-javascript
    • how-do-you-create-a-constructor-function
    • how-do-you-reliably-determine-whether-an-object-is-empty
    • what-advantage-is-there-for-using-the-arrow-syntax-for-a-method-in-a-constructor
    • what-are-callback-functions-and-how-are-they-used
    • what-are-default-parameters-and-how-are-they-used
    • what-are-promises-and-how-do-they-work
    • what-are-proxies-in-javascript-used-for
    • what-are-rest-parameters-and-how-are-they-used
    • what-are-template-literals-and-how-are-they-used
    • what-are-the-advantages-of-using-the-spread-operator-with-arrays-and-objects
    • what-are-the-benefits-of-using-spread-syntax-and-how-is-it-different-from-rest-syntax
    • what-are-the-differences-between-es6-class-and-es5-function-constructors
    • what-are-the-differences-between-variables-created-using-let-var-or-const
    • what-are-the-different-methods-for-iterating-over-an-array
    • what-are-the-different-ways-to-copy-an-object-or-an-array
    • what-are-the-various-data-types-in-javascript
    • what-are-the-various-ways-to-create-objects-in-javascript
    • what-is-recursion-and-how-is-it-used-in-javascript
    • what-is-the-definition-of-a-higher-order-function
    • what-is-the-difference-between-a-parameter-and-an-argument
    • what-is-the-difference-between-double-equal-and-triple-equal
    • what-is-the-purpose-of-the-break-and-continue-statements
    • what-is-the-purpose-of-the-new-keyword
    • what-is-the-purpose-of-the-switch-statement
    • what-is-the-spread-operator-and-how-is-it-used
    • what-is-the-ternary-operator-and-how-is-it-used
    • what-language-constructs-do-you-use-for-iterating-over-object-properties-and-array-items
    • whats-the-difference-between-a-variable-that-is-null-undefined-or-undeclared-how-would-you-go-about-checking-for-any-of-these-states
    • whats-the-difference-between-call-and-apply
  • scripts

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+904
-615
lines changed

README.md

Lines changed: 248 additions & 177 deletions
Large diffs are not rendered by default.

questions/can-you-offer-a-use-case-for-the-new-arrow-function-syntax-how-does-this-new-syntax-differ-from-other-functions/en-US.mdx

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ subtitle: How does this new syntax differ from other functions?
77

88
Arrow functions provide a concise syntax for writing functions in JavaScript. They are particularly useful for maintaining the `this` context within methods and callbacks. For example, in an event handler or array method like `map`, arrow functions can simplify the code and avoid issues with `this` binding.
99

10-
```javascript
10+
```js live
1111
const numbers = [1, 2, 3];
1212
const doubled = numbers.map((n) => n * 2);
1313
console.log(doubled); // [2, 4, 6]
@@ -21,30 +21,35 @@ console.log(doubled); // [2, 4, 6]
2121

2222
Arrow functions provide a more concise way to write functions. This is especially useful for short functions or callbacks.
2323

24-
```javascript
24+
```js live
2525
// Traditional function
2626
const add = function (a, b) {
2727
return a + b;
2828
};
2929

3030
// Arrow function
31-
const add = (a, b) => a + b;
31+
const anotherAdd = (a, b) => a + b;
32+
33+
console.log(add(2, 3)); // Output: 5
34+
console.log(anotherAdd(2, 3)); // Output: 5
3235
```
3336

3437
### Lexical `this` binding
3538

3639
Arrow functions do not have their own `this` context. Instead, they inherit `this` from the surrounding scope. This is particularly useful in methods and callbacks where the `this` context can be tricky.
3740

38-
```javascript
41+
```js live
3942
function Timer() {
4043
this.seconds = 0;
41-
setInterval(() => {
42-
this.seconds++;
44+
this.increment = () => {
45+
this.seconds++; // 'this.seconds' is inherited from the outer scope
4346
console.log(this.seconds);
44-
}, 1000);
47+
};
4548
}
4649

4750
const timer = new Timer();
51+
timer.increment(); // 1
52+
timer.increment(); // 2
4853
```
4954

5055
In the example above, using a traditional function inside `setInterval` would require additional steps to maintain the correct `this` context.
@@ -53,11 +58,11 @@ In the example above, using a traditional function inside `setInterval` would re
5358

5459
Arrow functions are often used in array methods like `map`, `filter`, and `reduce` for cleaner and more readable code.
5560

56-
```javascript
61+
```js live
5762
const numbers = [1, 2, 3, 4, 5];
5863

5964
// Traditional function
60-
const doubled = numbers.map(function (n) {
65+
const doubledTraditional = numbers.map(function (n) {
6166
return n * 2;
6267
});
6368

@@ -71,7 +76,7 @@ console.log(doubled); // [2, 4, 6, 8, 10]
7176

7277
Arrow functions can be used in event handlers to maintain the `this` context of the class or object.
7378

74-
```javascript
79+
```js
7580
class Button {
7681
constructor() {
7782
this.count = 0;

questions/difference-between-function-person-var-person-person-and-var-person-new-person/en-US.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ This code defines a function named `Person` that takes a parameter `name` and as
3333

3434
`const person = Person()` simply invoke the function's code. When you invoke `Person` as a regular function (i.e., without the `new` keyword), the function does not behave as a constructor. Instead, it executes its code and returns `undefined` if no return value is specified and that gets assigned to the variable intended as the instance. Invoking as such is a common mistake if the function is intended to be used as a constructor.
3535

36-
```js
36+
```js live
3737
function Person(name) {
3838
this.name = name;
3939
}
4040

41-
const person = Person('John');
41+
const person = Person('John'); // Throws error in strict mode
4242
console.log(person); // undefined
4343
console.log(person.name); // Uncaught TypeError: Cannot read property 'name' of undefined
4444
```
@@ -49,7 +49,7 @@ In this case, `Person('John')` does not create a new object. The `person` variab
4949

5050
`const person = new Person()` creates an instance of the `Person` object using the new operator, which inherits from `Person.prototype`. An alternative would be to use `Object.create`, such as: `Object.create(Person.prototype)` and `Person.call(person, 'John')` initializes the object.
5151

52-
```js
52+
```js live
5353
function Person(name) {
5454
this.name = name;
5555
}

questions/explain-hoisting/en-US.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ The following behavior summarizes the result of accessing the variables before t
2929

3030
## Hoisting
3131

32-
Hoisting is a term used to explain the behavior of variable declarations in JavaScript code.
32+
Hoisting is a term used to explain the behavior of variable declarations in JavaScript code.
3333

3434
Variables declared or initialized with the `var` keyword will have their declaration "moved" up to the top of their containing scope during compilation, which we refer to as hoisting.
3535

@@ -124,7 +124,7 @@ In ECMAScript specifications `let` and `const` declarations are [explained as be
124124

125125
> The variables are created when their containing Environment Record is instantiated but may not be accessed in any way until the variable's LexicalBinding is evaluated.
126126
127-
However, this statement is [a litle bit different for the `var` keyword](https://tc39.es/ecma262/#sec-variable-statement):
127+
However, this statement is [a little different for the `var` keyword](https://tc39.es/ecma262/#sec-variable-statement):
128128

129129
> Var variables are created when their containing Environment Record is instantiated and are initialized to `undefined` when created.
130130

questions/explain-how-prototypal-inheritance-works/en-US.mdx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ This behavior simulates classical inheritance, but it is really more of [delegat
1212

1313
Here's an example of prototypal inheritance:
1414

15-
```js
15+
```js live
1616
// Parent object constructor.
1717
function Animal(name) {
1818
this.name = name;
@@ -60,7 +60,7 @@ Prototypical inheritance is a feature in JavaScript used to create objects that
6060

6161
1. **Prototypes** : Every object in Javascript has a prototype, which is another object. When you create an object using an object literal or a constructor function, the new object is linked to the prototype of its constructor function or the `Object.prototype` if no prototype is specified. This is commonly referenced using `__proto__` or `[[Prototype]]`. You can also get the prototype by using inbuilt method `Object.getPrototypeOf()` and you can set the prototype of an object via `Object.setPrototypeOf()`.
6262

63-
```js
63+
```js live
6464
// Define a constructor function
6565
function Person(name, age) {
6666
this.name = name;
@@ -104,7 +104,7 @@ console.log(john.sayHello); // undefined
104104

105105
3. **Constructor functions**: JavaScript provides constructor functions to create objects. When a function is used as a constructor with the new keyword, the new object's prototype (`[[Prototype]]`) is set to the constructor's prototype property.
106106

107-
```js
107+
```js live
108108
// Define a constructor function
109109
function Animal(name) {
110110
this.name = name;
@@ -142,7 +142,7 @@ console.log(fido.fly); // undefined
142142

143143
4. **`Object.create()`**: This method creates a new object with the specified prototype object and properties. It's a straightforward way to set up prototypical inheritance. If you create a object via `Object.create(null)` it will not inherit any properties from `Object.prototype`. This means the object will not have any built-in properties or methods like `toString()`, `hasOwnProperty()`,
144144

145-
```js
145+
```js live
146146
// Define a prototype object
147147
let proto = {
148148
greet: function () {

questions/explain-the-concept-of-a-callback-function-in-asynchronous-operations/en-US.mdx

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ A callback function is a function that is passed as an argument to another funct
3232

3333
### Example of a synchronous callback
3434

35-
```js
35+
```js live
3636
function greet(name, callback) {
3737
console.log('Hello ' + name);
3838
callback();
@@ -76,13 +76,11 @@ fetchData((data) => {
7676

7777
When dealing with asynchronous operations, it's important to handle errors properly. A common pattern is to use the first argument of the callback function to pass an error object, if any.
7878

79-
```js
79+
```js live
8080
function fetchData(callback) {
81-
setTimeout(() => {
82-
const error = null;
83-
const data = { name: 'John', age: 30 };
84-
callback(error, data);
85-
}, 1000);
81+
// assume asynchronous operation to fetch data
82+
const { data, error } = { data: { name: 'John', age: 30 }, error: null };
83+
callback(error, data);
8684
}
8785

8886
fetchData((error, data) => {

questions/explain-the-concept-of-destructuring-assignment-for-objects-and-arrays/en-US.mdx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ title: Explain the concept of destructuring assignment for objects and arrays
66

77
Destructuring assignment is a syntax in JavaScript that allows you to unpack values from arrays or properties from objects into distinct variables. For arrays, you use square brackets, and for objects, you use curly braces. For example:
88

9-
```js
9+
```js live
1010
// Array destructuring
1111
const [a, b] = [1, 2];
1212

@@ -26,7 +26,7 @@ Array destructuring allows you to unpack values from arrays into distinct variab
2626

2727
#### Basic example
2828

29-
```js
29+
```js live
3030
const numbers = [1, 2, 3];
3131
const [first, second, third] = numbers;
3232

@@ -39,7 +39,7 @@ console.log(third); // 3
3939

4040
You can skip values in the array by leaving an empty space between commas.
4141

42-
```js
42+
```js live
4343
const numbers = [1, 2, 3];
4444
const [first, , third] = numbers;
4545

@@ -51,7 +51,7 @@ console.log(third); // 3
5151

5252
You can assign default values in case the array does not have enough elements.
5353

54-
```js
54+
```js live
5555
const numbers = [1];
5656
const [first, second = 2] = numbers;
5757

@@ -65,7 +65,7 @@ Object destructuring allows you to unpack properties from objects into distinct
6565

6666
#### Basic example
6767

68-
```js
68+
```js live
6969
const person = { name: 'John', age: 30 };
7070
const { name, age } = person;
7171

@@ -77,7 +77,7 @@ console.log(age); // 30
7777

7878
You can rename the variables while destructuring.
7979

80-
```js
80+
```js live
8181
const person = { name: 'John', age: 30 };
8282
const { name: personName, age: personAge } = person;
8383

@@ -89,7 +89,7 @@ console.log(personAge); // 30
8989

9090
You can assign default values in case the property does not exist in the object.
9191

92-
```js
92+
```js live
9393
const person = { name: 'John' };
9494
const { name, age = 25 } = person;
9595

@@ -101,7 +101,7 @@ console.log(age); // 25
101101

102102
You can destructure nested objects as well.
103103

104-
```js
104+
```js live
105105
const person = { name: 'John', address: { city: 'New York', zip: '10001' } };
106106
const {
107107
name,

questions/explain-the-concept-of-hoisting-with-regards-to-functions/en-US.mdx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ title: Explain the concept of hoisting with regards to functions
66

77
Hoisting in JavaScript is a behavior where function declarations are moved to the top of their containing scope during the compile phase. This means you can call a function before it is defined in the code. However, this does not apply to function expressions or arrow functions, which are not hoisted in the same way.
88

9-
```js
9+
```js live
1010
// Function declaration
1111
hoistedFunction(); // Works fine
1212
function hoistedFunction() {
@@ -30,7 +30,7 @@ Hoisting is a JavaScript mechanism where variables and function declarations are
3030

3131
Function declarations are fully hoisted. This means you can call a function before its declaration in the code.
3232

33-
```js
33+
```js live
3434
hoistedFunction(); // Works fine
3535

3636
function hoistedFunction() {
@@ -42,7 +42,7 @@ function hoistedFunction() {
4242

4343
Function expressions, including arrow functions, are not hoisted in the same way. They are treated as variable assignments and are only hoisted as undefined.
4444

45-
```js
45+
```js live
4646
nonHoistedFunction(); // Throws an error: TypeError: nonHoistedFunction is not a function
4747

4848
var nonHoistedFunction = function () {
@@ -54,7 +54,7 @@ var nonHoistedFunction = function () {
5454

5555
Arrow functions behave similarly to function expressions in terms of hoisting.
5656

57-
```js
57+
```js live
5858
arrowFunction(); // Throws an error: TypeError: arrowFunction is not a function
5959

6060
var arrowFunction = () => {

questions/explain-the-concept-of-inheritance-in-es2015-classes/en-US.mdx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ title: Explain the concept of inheritance in ES2015 classes
66

77
Inheritance in ES2015 classes allows one class to extend another, enabling the child class to inherit properties and methods from the parent class. This is done using the `extends` keyword. The `super` keyword is used to call the constructor and methods of the parent class. Here's a quick example:
88

9-
```js
9+
```js live
1010
class Animal {
1111
constructor(name) {
1212
this.name = name;
@@ -44,7 +44,7 @@ Inheritance in ES2015 classes allows a class (child class) to inherit properties
4444

4545
The `extends` keyword is used to create a class that is a child of another class. The child class inherits all the properties and methods of the parent class.
4646

47-
```js
47+
```js live
4848
class ParentClass {
4949
constructor() {
5050
this.parentProperty = 'I am a parent property';
@@ -75,7 +75,7 @@ child.parentMethod(); // This is a parent method
7575

7676
The `super` keyword is used to call the constructor of the parent class and to access its methods. This is necessary when you want to initialize the parent class properties in the child class.
7777

78-
```js
78+
```js live
7979
class Animal {
8080
constructor(name) {
8181
this.name = name;
@@ -108,7 +108,7 @@ dog.speak();
108108

109109
Child classes can override methods from the parent class. This allows the child class to provide a specific implementation of a method that is already defined in the parent class.
110110

111-
```js
111+
```js live
112112
class Animal {
113113
speak() {
114114
console.log('Animal makes a noise.');

questions/explain-the-concept-of-lexical-scoping/en-US.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ title: Explain the concept of lexical scoping
66

77
Lexical scoping means that the scope of a variable is determined by its location within the source code, and nested functions have access to variables declared in their outer scope. For example:
88

9-
```js
9+
```js live
1010
function outerFunction() {
1111
let outerVariable = 'I am outside!';
1212

@@ -36,7 +36,7 @@ When a function is defined, it captures the scope in which it was created. This
3636

3737
Consider the following example:
3838

39-
```js
39+
```js live
4040
function outerFunction() {
4141
let outerVariable = 'I am outside!';
4242

@@ -60,7 +60,7 @@ In this example:
6060

6161
Lexical scoping is closely related to closures. A closure is created when a function retains access to its lexical scope, even when the function is executed outside that scope.
6262

63-
```js
63+
```js live
6464
function outerFunction() {
6565
let outerVariable = 'I am outside!';
6666

0 commit comments

Comments
 (0)