JS 2025 Flashcards

1
Q

What’s the best way to clone an Object?

A

structuredClone(objectName)

alternative: JSON.parse(JSON.stringify(objectName) and assign to a new object (=)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you protect an Object from any modifications? No changes to values, no new/deleted properties?

A

Object.freeze(objectName)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you protect an Object from new/deleted properties

A

Object.seal(objectName)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you protect an Object from new properties only?

A

Object.preventExtensions(objectName)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you compare the Objects properly?

A
  1. JSON.stringify(object1) === JSON.stringify(object2)
  2. _.isEqual(obj1, obj2)

Objects need to be converted to strings to be compared

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Iterating through an object dynamically

People often loop using for…in, but a better way is to get all Keys

A

console.log(Object.keys(product));
// ✅ [“name”, “specs”]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Iterating through an object dynamically

People often loop using for…in, but a better way is to get all Values

A

console.log(Object.values(product));
// ✅ [“Laptop”, { ram: “16GB”, storage: “512GB” }]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Iterating through an object dynamically

People often loop using for…in, but a better way is to get all Keys & Values

A

console.log(Object.entries(product));
// ✅ [[“name”, “Laptop”], [“specs”, { ram: “16GB”, storage: “512GB” }]]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does the for…of loop do in JavaScript?

A

It loops through the values of an array without needing an index.

let numbers = [10, 20, 30];
for (let num of numbers) {
console.log(num); // Prints 10, then 20, then 30
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What does break do inside a loop?

A

It stops the loop immediately when a condition is met.

for (let i = 0; i < 10; i++) {
if (i === 5) break; // Stops the loop when i is 5
console.log(i);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Why use -Infinity when finding a maximum number?

A

-Infinity ensures any number is larger than it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does && do inside an if statement?

A

It checks two conditions at the same time.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Can multiple if statements run inside a function?

A

Yes! Unlike if-else, all if conditions will be checked.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

splice() heuristic for how each element works

A

arr.splice(index, howManyToRemove, valueToInsert)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly