You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+30-23Lines changed: 30 additions & 23 deletions
Original file line number
Diff line number
Diff line change
@@ -1494,7 +1494,7 @@ There are multiple ways to iterate over object properties as well as arrays in J
1494
1494
1495
1495
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
1496
1496
1497
-
```js
1497
+
```js live
1498
1498
constobj= {
1499
1499
a:1,
1500
1500
b:2,
@@ -1513,7 +1513,7 @@ for (const key in obj) {
1513
1513
1514
1514
`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.
1515
1515
1516
-
```js
1516
+
```js live
1517
1517
constobj= {
1518
1518
a:1,
1519
1519
b:2,
@@ -1529,7 +1529,7 @@ Most common ways to iterate over array are using `for` loop and `Array.prototype
1529
1529
1530
1530
**Using `for` loop**
1531
1531
1532
-
```js
1532
+
```js live
1533
1533
let array = [1, 2, 3, 4, 5, 6];
1534
1534
for (let index =0; index <array.length; index++) {
1535
1535
console.log(array[index]);
@@ -1538,7 +1538,7 @@ for (let index = 0; index < array.length; 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.
1551
1551
1552
-
```js
1552
+
```js live
1553
1553
constnumbers= [1, 2, 3, 4, 5];
1554
1554
1555
1555
for (constnumberof numbers) {
@@ -2322,9 +2322,9 @@ Template literals are a feature in JavaScript that allow for easier string inter
2322
2322
2323
2323
Example:
2324
2324
2325
-
```js
2326
-
constname='John';
2327
-
constgreeting=`Hello, ${name}!`;
2325
+
```js live
2326
+
constmyName='John';
2327
+
constgreeting=`Hello, ${myName}!`;
2328
2328
console.log(greeting); // Output: Hello, John!
2329
2329
```
2330
2330
@@ -2365,14 +2365,16 @@ console.log(result); // "Hello world! How are you?"
2365
2365
2366
2366
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.
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.
@@ -2615,7 +2618,7 @@ There are multiple ways to iterate over object properties as well as arrays in J
2615
2618
2616
2619
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
2617
2620
2618
-
```js
2621
+
```js live
2619
2622
constobj= {
2620
2623
a:1,
2621
2624
b:2,
@@ -2634,7 +2637,7 @@ for (const key in obj) {
2634
2637
2635
2638
`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.
2636
2639
2637
-
```js
2640
+
```js live
2638
2641
constobj= {
2639
2642
a:1,
2640
2643
b:2,
@@ -2650,7 +2653,7 @@ Most common ways to iterate over array are using `for` loop and `Array.prototype
2650
2653
2651
2654
**Using `for` loop**
2652
2655
2653
-
```js
2656
+
```js live
2654
2657
let array = [1, 2, 3, 4, 5, 6];
2655
2658
for (let index =0; index <array.length; index++) {
2656
2659
console.log(array[index]);
@@ -2659,7 +2662,7 @@ for (let index = 0; index < array.length; 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.
2672
2675
2673
-
```js
2676
+
```js live
2674
2677
constnumbers= [1, 2, 3, 4, 5];
2675
2678
2676
2679
for (constnumberof numbers) {
@@ -2698,7 +2701,7 @@ There are also other inbuilt methods available which are suitable for specific s
2698
2701
2699
2702
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.
2700
2703
2701
-
```js
2704
+
```js live
2702
2705
for (let i =0; i <10; i++) {
2703
2706
if (i ===5) break; // exits the loop when i is 5
2704
2707
console.log(i);
@@ -2738,7 +2741,7 @@ The ternary operator is a shorthand for an `if-else` statement in JavaScript. It
2738
2741
2739
2742
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`:
2740
2743
2741
-
```js
2744
+
```js live
2742
2745
constarray= ['a', 'b', 'c'];
2743
2746
array.forEach((element, index) => {
2744
2747
console.log(index, element);
@@ -2786,7 +2789,7 @@ switch (expression) {
2786
2789
2787
2790
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.
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.
@@ -3005,7 +3012,7 @@ A parameter is a variable in the declaration of a function, while an argument is
3005
3012
3006
3013
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.
Copy file name to clipboardExpand all lines: questions/explain-the-concept-of-hoisting-with-regards-to-functions/en-US.mdx
+4-4Lines changed: 4 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ title: Explain the concept of hoisting with regards to functions
6
6
7
7
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.
8
8
9
-
```js
9
+
```js live
10
10
// Function declaration
11
11
hoistedFunction(); // Works fine
12
12
functionhoistedFunction() {
@@ -30,7 +30,7 @@ Hoisting is a JavaScript mechanism where variables and function declarations are
30
30
31
31
Function declarations are fully hoisted. This means you can call a function before its declaration in the code.
32
32
33
-
```js
33
+
```js live
34
34
hoistedFunction(); // Works fine
35
35
36
36
functionhoistedFunction() {
@@ -42,7 +42,7 @@ function hoistedFunction() {
42
42
43
43
Function expressions, including arrow functions, are not hoisted in the same way. They are treated as variable assignments and are only hoisted as undefined.
44
44
45
-
```js
45
+
```js live
46
46
nonHoistedFunction(); // Throws an error: TypeError: nonHoistedFunction is not a function
47
47
48
48
varnonHoistedFunction=function () {
@@ -54,7 +54,7 @@ var nonHoistedFunction = function () {
54
54
55
55
Arrow functions behave similarly to function expressions in terms of hoisting.
56
56
57
-
```js
57
+
```js live
58
58
arrowFunction(); // Throws an error: TypeError: arrowFunction is not a function
Copy file name to clipboardExpand all lines: questions/explain-the-concept-of-the-spread-operator-and-its-uses/en-US.mdx
+10-6Lines changed: 10 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -6,22 +6,26 @@ title: Explain the concept of the spread operator and its uses
6
6
7
7
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.
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.
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.
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.
Copy file name to clipboardExpand all lines: questions/explain-the-difference-in-hoisting-between-var-let-and-const/en-US.mdx
+3-3Lines changed: 3 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -14,7 +14,7 @@ title: Explain the difference in hoisting between `var`, `let`, and `const`
14
14
15
15
`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.
16
16
17
-
```js
17
+
```js live
18
18
console.log(a); // Output: undefined
19
19
var a =10;
20
20
console.log(a); // Output: 10
@@ -24,7 +24,7 @@ console.log(a); // Output: 10
24
24
25
25
`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`.
26
26
27
-
```js
27
+
```js live
28
28
console.log(b); // ReferenceError: Cannot access 'b' before initialization
29
29
let b =20;
30
30
console.log(b); // Output: 20
@@ -34,7 +34,7 @@ console.log(b); // Output: 20
34
34
35
35
`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.
36
36
37
-
```js
37
+
```js live
38
38
console.log(c); // ReferenceError: Cannot access 'c' before initialization
0 commit comments