Skip to content

Executable coding blocks production-readiness - PR 2 #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 57 commits into from
Mar 9, 2025
Merged
Show file tree
Hide file tree
Changes from 53 commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
3b0f7b6
exec: how-do-you-convert-a-string-to-a-number-in-javascript
tahachm Mar 1, 2025
df35cdd
exec: what-are-the-various-data-types-in-javascript
tahachm Mar 2, 2025
8278bc1
exec: explain-the-concept-of-hoisting-with-regards-to-functions
tahachm Mar 2, 2025
66da940
exec: explain-the-difference-in-hoisting-between-var-let-and-const
tahachm Mar 2, 2025
377fded
exec: what-is-the-spread-operator-and-how-is-it-used
tahachm Mar 2, 2025
03d6fcd
exec: how-do-you-check-the-data-type-of-a-variable
tahachm Mar 2, 2025
c036438
exec: what-are-the-differences-between-variables-created-using-let-va…
tahachm Mar 2, 2025
d6f3b0e
exec: whats-the-difference-between-a-variable-that-is-null-undefined-…
tahachm Mar 2, 2025
15d0f04
exec: what-are-template-literals-and-how-are-they-used
tahachm Mar 2, 2025
e3f2fe4
exec: how-can-you-avoid-problems-related-to-hoisting
tahachm Mar 2, 2025
c976667
exec: what-is-the-difference-between-double-equal-and-triple-equal
tahachm Mar 2, 2025
f9b6c18
exec: what-language-constructs-do-you-use-for-iterating-over-object-p…
tahachm Mar 2, 2025
1a59dbd
exec: what-is-the-purpose-of-the-break-and-continue-statements
tahachm Mar 2, 2025
93fc6c7
exec: what-is-the-ternary-operator-and-how-is-it-used
tahachm Mar 2, 2025
52b9eb3
exec: how-do-you-access-the-index-of-an-element-in-an-array-during-it…
tahachm Mar 2, 2025
3e044c8
exec: what-is-the-purpose-of-the-switch-statement
tahachm Mar 2, 2025
5917f8d
exec: what-are-rest-parameters-and-how-are-they-used
tahachm Mar 2, 2025
b3b8e22
exec: explain-the-concept-of-the-spread-operator-and-its-uses
tahachm Mar 2, 2025
f0f1c01
[auto] regenerate table of contents
github-actions[bot] Mar 2, 2025
be56334
exec: what-are-the-benefits-of-using-spread-syntax-and-how-is-it-diff…
tahachm Mar 6, 2025
f61faa7
exec: explain-the-differences-on-the-usage-of-foo-between-function-fo…
tahachm Mar 6, 2025
730aee9
exec: what-is-the-difference-between-a-parameter-and-an-argument
tahachm Mar 6, 2025
7cf0b24
exec: whats-the-difference-between-call-and-apply
tahachm Mar 6, 2025
40c09e4
partial fix: can-you-offer-a-use-case-for-the-new-arrow-function-synt…
tahachm Mar 6, 2025
1b56553
exec: difference-between-function-person-var-person-person-and-var-pe…
tahachm Mar 6, 2025
86aabc6
partial fix: what-is-the-definition-of-a-higher-order-function
tahachm Mar 6, 2025
d837bb0
exec: what-is-recursion-and-how-is-it-used-in-javascript
tahachm Mar 6, 2025
f8a5f10
exec: what-are-default-parameters-and-how-are-they-used
tahachm Mar 6, 2025
75b98d6
exec: what-are-the-various-ways-to-create-objects-in-javascript
tahachm Mar 6, 2025
9d624e4
exec: explain-the-difference-between-dot-notation-and-bracket-notatio…
tahachm Mar 6, 2025
08881bc
exec: what-are-the-different-methods-for-iterating-over-an-array
tahachm Mar 6, 2025
a758de0
exec: how-do-you-add-remove-and-update-elements-in-an-array
tahachm Mar 6, 2025
c945e2b
partial fix: what-are-the-different-ways-to-copy-an-object-or-an-array
tahachm Mar 6, 2025
a7b0bbc
exec: explain-the-difference-between-shallow-copy-and-deep-copy
tahachm Mar 6, 2025
7e7cfb0
exec: what-are-the-advantages-of-using-the-spread-operator-with-array…
tahachm Mar 6, 2025
5f53908
exec: how-do-you-check-if-an-object-has-a-specific-property
tahachm Mar 6, 2025
4b75530
exec: explain-the-concept-of-destructuring-assignment-for-objects-and…
tahachm Mar 6, 2025
0983882
exec: how-do-you-reliably-determine-whether-an-object-is-empty
tahachm Mar 6, 2025
8a7e7f1
partial fix: explain-the-concept-of-a-callback-function-in-asynchrono…
tahachm Mar 6, 2025
b961378
partial fix: what-are-promises-and-how-do-they-work
tahachm Mar 6, 2025
6bc474a
partial fix: what-are-callback-functions-and-how-are-they-used
tahachm Mar 6, 2025
4235698
partial fix: explain-the-difference-between-synchronous-and-asynchron…
tahachm Mar 6, 2025
9c4464c
exec: explain-how-prototypal-inheritance-works
tahachm Mar 7, 2025
3656d64
exec: explain-the-difference-between-classical-inheritance-and-protot…
tahachm Mar 7, 2025
38f2a07
exec: explain-the-concept-of-inheritance-in-es2015-classes
tahachm Mar 7, 2025
7a1b2be
exec: what-is-the-purpose-of-the-new-keyword
tahachm Mar 7, 2025
4b864fa
exec: how-do-you-create-a-constructor-function
tahachm Mar 7, 2025
208848a
exec: what-are-the-differences-between-es6-class-and-es5-function-con…
tahachm Mar 7, 2025
ed92473
exec: explain-the-concept-of-lexical-scoping
tahachm Mar 7, 2025
ec00257
exec: explain-the-concept-of-scope-in-javascript
tahachm Mar 7, 2025
aca5590
exec: how-can-closures-be-used-to-create-private-variables
tahachm Mar 7, 2025
14f54cf
exec: explain-the-difference-between-global-scope-function-scope-and-…
tahachm Mar 7, 2025
0596b5c
partial fix: what-advantage-is-there-for-using-the-arrow-syntax-for-a…
tahachm Mar 7, 2025
9925c12
[auto] regenerate table of contents
github-actions[bot] Mar 7, 2025
f0a6605
Merge branch 'main' into taha/executable-coding-blocks-2
yangshun Mar 9, 2025
b5fcd8c
Update en-US.mdx
yangshun Mar 9, 2025
3fcfe79
[auto] regenerate table of contents
github-actions[bot] Mar 9, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 30 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1494,7 +1494,7 @@ There are multiple ways to iterate over object properties as well as arrays in J

The `for...in` loop iterates over all enumerable properties of an object, including inherited enumerable properties. So it is important to have a check if you only want to iterate over object's own properties

```js
```js live
const obj = {
a: 1,
b: 2,
Expand All @@ -1513,7 +1513,7 @@ for (const key in obj) {

`Object.keys()` returns an array of the object's own enumerable property names. You can then use a for...of loop or forEach to iterate over this array.

```js
```js live
const obj = {
a: 1,
b: 2,
Expand All @@ -1529,7 +1529,7 @@ Most common ways to iterate over array are using `for` loop and `Array.prototype

**Using `for` loop**

```js
```js live
let array = [1, 2, 3, 4, 5, 6];
for (let index = 0; index < array.length; index++) {
console.log(array[index]);
Expand All @@ -1538,7 +1538,7 @@ for (let index = 0; index < array.length; index++) {

**Using `Array.prototype.forEach` method**

```js
```js live
let array = [1, 2, 3, 4, 5, 6];
array.forEach((number, index) => {
console.log(`${number} at index ${index}`);
Expand All @@ -1549,7 +1549,7 @@ array.forEach((number, index) => {

This method is the newest and most convenient way to iterate over arrays. It automatically iterates over each element without requiring you to manage the index.

```js
```js live
const numbers = [1, 2, 3, 4, 5];

for (const number of numbers) {
Expand Down Expand Up @@ -2322,9 +2322,9 @@ Template literals are a feature in JavaScript that allow for easier string inter

Example:

```js
const name = 'John';
const greeting = `Hello, ${name}!`;
```js live
const myName = 'John';
const greeting = `Hello, ${myName}!`;
console.log(greeting); // Output: Hello, John!
```

Expand Down Expand Up @@ -2365,14 +2365,16 @@ console.log(result); // "Hello world! How are you?"

The spread operator, represented by three dots (`...`), is used in JavaScript to expand iterable objects like arrays or strings into individual elements. It can also be used to spread object properties. For example, you can use it to combine arrays, copy arrays, or pass array elements as arguments to a function.

```js
```js live
const arr1 = [1, 2, 3];
const arr2 = [4, 5, 6];
const combined = [...arr1, ...arr2]; // [1, 2, 3, 4, 5, 6]
const combined = [...arr1, ...arr2];
console.log(combined); // [1, 2, 3, 4, 5, 6]

const obj1 = { a: 1, b: 2 };
const obj2 = { c: 3, d: 4 };
const combinedObj = { ...obj1, ...obj2 }; // { a: 1, b: 2, c: 3, d: 4 }
const combinedObj = { ...obj1, ...obj2 };
console.log(combinedObj); // { a: 1, b: 2, c: 3, d: 4 }
```

<!-- Update here: /questions/what-is-the-spread-operator-and-how-is-it-used/en-US.mdx -->
Expand Down Expand Up @@ -2565,16 +2567,17 @@ let b = 10;

To avoid problems related to hoisting, always declare variables at the top of their scope using `let` or `const` instead of `var`. This ensures that variables are block-scoped and not hoisted to the top of their containing function or global scope. Additionally, declare functions before they are called to avoid issues with function hoisting.

```js
```js live
// Use let or const
let x = 10;
const y = 20;
console.log(x, y); // Output: 10 20

// Declare functions before calling them
function myFunction() {
console.log('Hello, world!');
}
myFunction();
myFunction(); // Output: 'Hello, world!'
```

<!-- Update here: /questions/how-can-you-avoid-problems-related-to-hoisting/en-US.mdx -->
Expand Down Expand Up @@ -2615,7 +2618,7 @@ There are multiple ways to iterate over object properties as well as arrays in J

The `for...in` loop iterates over all enumerable properties of an object, including inherited enumerable properties. So it is important to have a check if you only want to iterate over object's own properties

```js
```js live
const obj = {
a: 1,
b: 2,
Expand All @@ -2634,7 +2637,7 @@ for (const key in obj) {

`Object.keys()` returns an array of the object's own enumerable property names. You can then use a for...of loop or forEach to iterate over this array.

```js
```js live
const obj = {
a: 1,
b: 2,
Expand All @@ -2650,7 +2653,7 @@ Most common ways to iterate over array are using `for` loop and `Array.prototype

**Using `for` loop**

```js
```js live
let array = [1, 2, 3, 4, 5, 6];
for (let index = 0; index < array.length; index++) {
console.log(array[index]);
Expand All @@ -2659,7 +2662,7 @@ for (let index = 0; index < array.length; index++) {

**Using `Array.prototype.forEach` method**

```js
```js live
let array = [1, 2, 3, 4, 5, 6];
array.forEach((number, index) => {
console.log(`${number} at index ${index}`);
Expand All @@ -2670,7 +2673,7 @@ array.forEach((number, index) => {

This method is the newest and most convenient way to iterate over arrays. It automatically iterates over each element without requiring you to manage the index.

```js
```js live
const numbers = [1, 2, 3, 4, 5];

for (const number of numbers) {
Expand Down Expand Up @@ -2698,7 +2701,7 @@ There are also other inbuilt methods available which are suitable for specific s

The `break` statement is used to exit a loop or switch statement prematurely, while the `continue` statement skips the current iteration of a loop and proceeds to the next iteration. For example, in a `for` loop, `break` will stop the loop entirely, and `continue` will skip to the next iteration.

```js
```js live
for (let i = 0; i < 10; i++) {
if (i === 5) break; // exits the loop when i is 5
console.log(i);
Expand Down Expand Up @@ -2738,7 +2741,7 @@ The ternary operator is a shorthand for an `if-else` statement in JavaScript. It

To access the index of an element in an array during iteration, you can use methods like `forEach`, `map`, `for...of` with `entries`, or a traditional `for` loop. For example, using `forEach`:

```js
```js live
const array = ['a', 'b', 'c'];
array.forEach((element, index) => {
console.log(index, element);
Expand Down Expand Up @@ -2786,7 +2789,7 @@ switch (expression) {

Rest parameters in JavaScript allow a function to accept an indefinite number of arguments as an array. They are denoted by three dots (`...`) followed by the name of the array. This feature is useful for functions that need to handle multiple arguments without knowing the exact number in advance.

```js
```js live
function sum(...numbers) {
return numbers.reduce((acc, curr) => acc + curr, 0);
}
Expand All @@ -2808,22 +2811,26 @@ console.log(sum(1, 2, 3, 4)); // Output: 10

The spread operator (`...`) in JavaScript allows you to expand elements of an iterable (like an array or object) into individual elements. It is commonly used for copying arrays or objects, merging arrays or objects, and passing elements of an array as arguments to a function.

```js
```js live
// Copying an array
const arr1 = [1, 2, 3];
const arr2 = [...arr1];
console.log(arr2); // Output: [1, 2, 3]

// Merging arrays
const arr3 = [4, 5, 6];
const mergedArray = [...arr1, ...arr3];
console.log(mergedArray); // Output: [1, 2, 3, 4, 5, 6]

// Copying an object
const obj1 = { a: 1, b: 2 };
const obj2 = { ...obj1 };
console.log(obj2); // Output: { a: 1, b: 2 }

// Merging objects
const obj3 = { c: 3, d: 4 };
const mergedObject = { ...obj1, ...obj3 };
console.log(mergedObject); // Output: { a: 1, b: 2, c: 3, d: 4 }

// Passing array elements as function arguments
const sum = (x, y, z) => x + y + z;
Expand Down Expand Up @@ -3005,7 +3012,7 @@ A parameter is a variable in the declaration of a function, while an argument is

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.

```js
```js live
// Function declaration
hoistedFunction(); // Works fine
function hoistedFunction() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ subtitle: How does this new syntax differ from other functions?

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.

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

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

```javascript
```js live
// Traditional function
const add = function (a, b) {
return a + b;
};

// Arrow function
const add = (a, b) => a + b;
const anotherAdd = (a, b) => a + b;

console.log(add(2, 3)); // Output: 5
console.log(anotherAdd(2, 3)); // Output: 5
```

### Lexical `this` binding

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.

```javascript
```js live
function Timer() {
this.seconds = 0;
setInterval(() => {
this.seconds++;
this.increment = () => {
this.seconds++; // 'this.seconds' is inherited from the outer scope
console.log(this.seconds);
}, 1000);
};
}

const timer = new Timer();
timer.increment(); // 1
timer.increment(); // 2
```

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

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

```javascript
```js live
const numbers = [1, 2, 3, 4, 5];

// Traditional function
const doubled = numbers.map(function (n) {
const doubledTraditional = numbers.map(function (n) {
return n * 2;
});

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

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

```javascript
```js
class Button {
constructor() {
this.count = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ This code defines a function named `Person` that takes a parameter `name` and as

`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.

```js
```js live
function Person(name) {
this.name = name;
}

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

`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.

```js
```js live
function Person(name) {
this.name = name;
}
Expand Down
8 changes: 4 additions & 4 deletions questions/explain-how-prototypal-inheritance-works/en-US.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ This behavior simulates classical inheritance, but it is really more of [delegat

Here's an example of prototypal inheritance:

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

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()`.

```js
```js live
// Define a constructor function
function Person(name, age) {
this.name = name;
Expand Down Expand Up @@ -104,7 +104,7 @@ console.log(john.sayHello); // undefined

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.

```js
```js live
// Define a constructor function
function Animal(name) {
this.name = name;
Expand Down Expand Up @@ -142,7 +142,7 @@ console.log(fido.fly); // undefined

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()`,

```js
```js live
// Define a prototype object
let proto = {
greet: function () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ A callback function is a function that is passed as an argument to another funct

### Example of a synchronous callback

```js
```js live
function greet(name, callback) {
console.log('Hello ' + name);
callback();
Expand Down Expand Up @@ -76,13 +76,11 @@ fetchData((data) => {

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.

```js
```js live
function fetchData(callback) {
setTimeout(() => {
const error = null;
const data = { name: 'John', age: 30 };
callback(error, data);
}, 1000);
// assume asynchronous operation to fetch data
const { data, error } = { data: { name: 'John', age: 30 }, error: null };
callback(error, data);
}

fetchData((error, data) => {
Expand Down
Loading