Skip to content

Commit 9e3cebb

Browse files
committed
Day 8 js interview
1 parent f3b2a11 commit 9e3cebb

1 file changed

Lines changed: 135 additions & 0 deletions

File tree

JavaScript Interview/Day 8/demo.js

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
2+
/*
3+
4+
5+
Let me explain step-by-step in simpler terms. We'll break it down carefully:
6+
7+
Code:
8+
javascript
9+
Copy code
10+
let arr = [1, 2, 3, 4][1, 2, 3];
11+
console.log(arr);
12+
Step 1: Understand the array
13+
You have an array:
14+
15+
javascript
16+
Copy code
17+
[1, 2, 3, 4]
18+
The array has these values:
19+
Index 0 → 1
20+
Index 1 → 2
21+
Index 2 → 3
22+
Index 3 → 4
23+
Step 2: Understand [1, 2, 3]
24+
Inside the square brackets, you wrote:
25+
26+
javascript
27+
Copy code
28+
[1, 2, 3]
29+
This is a special situation because JavaScript treats this as an expression.
30+
31+
Step 3: What is an "expression"?
32+
An expression evaluates something and produces a value.
33+
34+
In JavaScript, when you write something like:
35+
javascript
36+
Copy code
37+
[1, 2, 3];
38+
It means:
39+
Look at all the numbers.
40+
Only use the last number in this list.
41+
So [1, 2, 3] evaluates to 3.
42+
Step 4: Replace [1, 2, 3] with 3
43+
Now your code becomes:
44+
45+
javascript
46+
Copy code
47+
let arr = [1, 2, 3, 4][3];
48+
Step 5: Access the value at index 3
49+
In the array [1, 2, 3, 4], the index 3 refers to the last element, which is 4.
50+
So:
51+
52+
javascript
53+
Copy code
54+
arr = 4;
55+
Step 6: Output the result
56+
Finally:
57+
58+
javascript
59+
Copy code
60+
console.log(arr); // Outputs: 4
61+
62+
1. let arr = [1, 2, 3, 4][1, 2, 3]; this is expression
63+
console.log(arr);// 4
64+
65+
66+
67+
2.
68+
map and forEach are both methods available in JavaScript arrays that allow you to perform operations on each element of the array. However, they are different in their purpose and behavior.
69+
70+
1. map Method
71+
The map method creates a new array by applying a callback function to each element of the original array.
72+
73+
Key Features:
74+
Returns a new array with the transformed values.
75+
Does not modify the original array.
76+
Used when you need to transform or derive new data from the original array.
77+
Example:
78+
javascript
79+
Copy code
80+
const numbers = [1, 2, 3, 4];
81+
const squared = numbers.map(num => num * num);
82+
83+
console.log(squared); // [1, 4, 9, 16]
84+
console.log(numbers); // [1, 2, 3, 4] (original array remains unchanged)
85+
2. forEach Method
86+
The forEach method executes a callback function for each element in the array. It does not return a new array.
87+
88+
Key Features:
89+
Does not return anything (undefined).
90+
Typically used for side effects, like logging, updating variables, or modifying elements in place.
91+
Does not break or return early (no return in forEach).
92+
Example:
93+
javascript
94+
Copy code
95+
const numbers = [1, 2, 3, 4];
96+
numbers.forEach(num => console.log(num * num));
97+
98+
// Output:
99+
// 1
100+
// 4
101+
// 9
102+
// 16
103+
104+
console.log(numbers); // [1, 2, 3, 4] (original array remains unchanged)
105+
106+
107+
108+
3. function fn() {
109+
console.log("hello")
110+
}
111+
112+
fn();// hello
113+
114+
115+
116+
4. function fn() {
117+
return 100;
118+
}
119+
120+
var fn;
121+
console.log(fn);// [Function: fn]
122+
123+
124+
5. class abb {
125+
126+
127+
}
128+
console.log(typeof(abb))// function
129+
130+
131+
6.
132+
console.log("a" -1)//NAN
133+
*/
134+
135+

0 commit comments

Comments
 (0)