Skip to content

Executable coding blocks production-readiness - PR 1 #20

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 19 commits into from
Mar 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 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
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 @@ -6,7 +6,7 @@ title: Explain the concept of hoisting with regards to functions

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 All @@ -30,7 +30,7 @@ Hoisting is a JavaScript mechanism where variables and function declarations are

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

```js
```js live
hoistedFunction(); // Works fine

function hoistedFunction() {
Expand All @@ -42,7 +42,7 @@ function hoistedFunction() {

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

```js
```js live
nonHoistedFunction(); // Throws an error: TypeError: nonHoistedFunction is not a function

var nonHoistedFunction = function () {
Expand All @@ -54,7 +54,7 @@ var nonHoistedFunction = function () {

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

```js
```js live
arrowFunction(); // Throws an error: TypeError: arrowFunction is not a function

var arrowFunction = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,26 @@ title: Explain the concept of the spread operator and its uses

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 All @@ -37,7 +41,7 @@ console.log(sum(...numbers)); // Output: 6

The spread operator can be used to create a shallow copy of an array. This is useful when you want to duplicate an array without affecting the original array.

```js
```js live
const arr1 = [1, 2, 3];
const arr2 = [...arr1];
console.log(arr2); // Output: [1, 2, 3]
Expand All @@ -47,7 +51,7 @@ console.log(arr2); // Output: [1, 2, 3]

You can use the spread operator to merge multiple arrays into one. This is a concise and readable way to combine arrays.

```js
```js live
const arr1 = [1, 2, 3];
const arr3 = [4, 5, 6];
const mergedArray = [...arr1, ...arr3];
Expand All @@ -58,7 +62,7 @@ console.log(mergedArray); // Output: [1, 2, 3, 4, 5, 6]

Similar to arrays, the spread operator can be used to create a shallow copy of an object. This is useful for duplicating objects without affecting the original object.

```js
```js live
const obj1 = { a: 1, b: 2 };
const obj2 = { ...obj1 };
console.log(obj2); // Output: { a: 1, b: 2 }
Expand All @@ -68,7 +72,7 @@ console.log(obj2); // Output: { a: 1, b: 2 }

The spread operator can also be used to merge multiple objects into one. This is particularly useful for combining properties from different objects.

```js
```js live
const obj1 = { a: 1, b: 2 };
const obj3 = { c: 3, d: 4 };
const mergedObject = { ...obj1, ...obj3 };
Expand All @@ -79,7 +83,7 @@ console.log(mergedObject); // Output: { a: 1, b: 2, c: 3, d: 4 }

The spread operator allows you to pass elements of an array as individual arguments to a function. This is useful for functions that accept multiple arguments.

```js
```js live
const sum = (x, y, z) => x + y + z;
const numbers = [1, 2, 3];
console.log(sum(...numbers)); // Output: 6
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ title: Explain the difference in hoisting between `var`, `let`, and `const`

`var` declarations are hoisted to the top of their containing function or global scope. This means the variable is available throughout the entire function or script, even before the line where it is declared. However, the variable is initialized with `undefined` until the actual declaration is encountered.

```js
```js live
console.log(a); // Output: undefined
var a = 10;
console.log(a); // Output: 10
Expand All @@ -24,7 +24,7 @@ console.log(a); // Output: 10

`let` declarations are also hoisted to the top of their block scope, but they are not initialized. This creates a "temporal dead zone" (TDZ) from the start of the block until the declaration is encountered. Accessing the variable in the TDZ results in a `ReferenceError`.

```js
```js live
console.log(b); // ReferenceError: Cannot access 'b' before initialization
let b = 20;
console.log(b); // Output: 20
Expand All @@ -34,7 +34,7 @@ console.log(b); // Output: 20

`const` declarations behave similarly to `let` in terms of hoisting. They are hoisted to the top of their block scope but are not initialized, resulting in a TDZ. Additionally, `const` requires an initial value at the time of declaration and cannot be reassigned.

```js
```js live
console.log(c); // ReferenceError: Cannot access 'c' before initialization
const c = 30;
console.log(c); // Output: 30
Expand Down
Loading