Skip to content

Commit 86b341e

Browse files
tahachmgithub-actions[bot]yangshun
authored
Executable coding blocks production-readiness - PR 2 (#22)
Co-authored-by: GitHub Actions <github-actions[bot]@users.noreply.github.com> Co-authored-by: Yangshun Tay <[email protected]>
1 parent 8535fd3 commit 86b341e

File tree

35 files changed

+558
-365
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-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-inheritance-in-es2015-classes
    • explain-the-concept-of-lexical-scoping
    • explain-the-concept-of-scope-in-javascript
    • 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-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-add-remove-and-update-elements-in-an-array
    • how-do-you-check-if-an-object-has-a-specific-property
    • 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-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-different-methods-for-iterating-over-an-array
    • what-are-the-different-ways-to-copy-an-object-or-an-array
    • 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-purpose-of-the-new-keyword
    • whats-the-difference-between-call-and-apply

35 files changed

+558
-365
lines changed

README.md

Lines changed: 161 additions & 98 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-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-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

questions/explain-the-concept-of-scope-in-javascript/en-US.mdx

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,33 @@ title: Explain the concept of scope in JavaScript
66

77
In JavaScript, scope determines the accessibility of variables and functions at different parts of the code. There are three main types of scope: global scope, function scope, and block scope. Global scope means the variable is accessible everywhere in the code. Function scope means the variable is accessible only within the function it is declared. Block scope, introduced with ES6, means the variable is accessible only within the block (e.g., within curly braces `{}`) it is declared.
88

9-
```js
10-
// Global scope
11-
var globalVar = 'I am global';
9+
```js live
10+
var globalVar = 'I am a global var';
1211

1312
function myFunction() {
14-
// Function scope
15-
var functionVar = 'I am in a function';
13+
var functionVar = 'I am a function-scoped var';
1614

1715
if (true) {
18-
// Block scope
19-
let blockVar = 'I am in a block';
20-
console.log(blockVar); // Accessible here
16+
let blockVar = 'I am a block-scoped var';
17+
18+
console.log('Inside block:');
19+
console.log(globalVar); // Accessible
20+
console.log(functionVar); // Accessible
21+
console.log(blockVar); // Accessible
2122
}
2223

23-
// console.log(blockVar); // Uncaught ReferenceError: blockVar is not defined
24+
console.log('Inside function:');
25+
console.log(globalVar); // Accessible
26+
console.log(functionVar); // Accessible
27+
// console.log(blockVar); // Uncaught ReferenceError
2428
}
2529

26-
console.log(globalVar); // Accessible here
27-
// console.log(functionVar); // Uncaught ReferenceError: functionVar is not defined
30+
myFunction();
31+
32+
console.log('In global scope:');
33+
console.log(globalVar); // Accessible
34+
// console.log(functionVar); // Uncaught ReferenceError
35+
// console.log(blockVar); // Uncaught ReferenceError
2836
```
2937

3038
---
@@ -35,47 +43,49 @@ console.log(globalVar); // Accessible here
3543

3644
Variables declared outside any function or block have global scope. They are accessible from anywhere in the code.
3745

38-
```js
46+
```js live
3947
var globalVar = 'I am global';
4048

4149
function myFunction() {
4250
console.log(globalVar); // Accessible here
4351
}
4452

53+
myFunction();
4554
console.log(globalVar); // Accessible here
4655
```
4756

4857
### Function scope
4958

5059
Variables declared within a function are in function scope. They are accessible only within that function.
5160

52-
```js
61+
```js live
5362
function myFunction() {
5463
var functionVar = 'I am in a function';
5564
console.log(functionVar); // Accessible here
5665
}
5766

58-
// console.log(functionVar); // Uncaught ReferenceError: functionVar is not defined
67+
myFunction();
68+
console.log(functionVar); // Uncaught ReferenceError: functionVar is not defined
5969
```
6070

6171
### Block scope
6272

6373
Variables declared with `let` or `const` within a block (e.g., within curly braces `{}`) have block scope. They are accessible only within that block.
6474

65-
```js
75+
```js live
6676
if (true) {
6777
let blockVar = 'I am in a block';
6878
console.log(blockVar); // Accessible here
6979
}
7080

71-
// console.log(blockVar); // Uncaught ReferenceError: blockVar is not defined
81+
console.log(blockVar); // Uncaught ReferenceError: blockVar is not defined
7282
```
7383

7484
### Lexical scope
7585

7686
JavaScript uses lexical scoping, meaning that the scope of a variable is determined by its location within the source code. Nested functions have access to variables declared in their outer scope.
7787

78-
```js
88+
```js live
7989
function outerFunction() {
8090
var outerVar = 'I am outside';
8191

0 commit comments

Comments
 (0)