-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPractice.js
More file actions
59 lines (45 loc) · 1.72 KB
/
Practice.js
File metadata and controls
59 lines (45 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// 1. What is the difference between == and === in JavaScript?
let a = 5;
let b = 5;
//console.log(a == b); // true, because == checks for value equality
//console.log(a === b); // false, because === checks for both value and type equality
//2. Explain Closures with a Simple Example
function getName(greeting) {
return function(name) {
console.log(greeting + ', ' + name);
}
}
// const greetHello = getName('Hello');
// greetHello('John'); // Output: Hello, John
//3. What is the Event Loop in JavaScript?
//The event loop allows JavaScript to execute non-blocking operations
//by managing the execution of callbacks in a queue after the call stack is empty.
console.log('Start');
setTimeout(() => {
console.log('Timeout');
}, 5000); // 5 seconds delay
console.log('End');
// Output: Start, End, Timeout
//4. Describe let, const, and var - What are the Differences?
//var is function-scoped and can be redeclared,
//let is block-scoped and can be reassigned,
//and const is block-scoped and cannot be reassigned.
// var - function scoped, can be redeclared
var x = 10;
var x = 20; // Allowed
// let - block scoped, cannot be redeclared in same scope
let y = 10;
// let y = 20; // This would cause an error
// const - block scoped, cannot be reassigned
const z = 10;
// z = 20; // This would cause an error
//5. What is Destructuring in JavaScript?
//Destructuring allows you to extract values from arrays or properties from objects into distinct variables using a concise syntax.
//javascript// Object Destructuring
const person = { name: 'John', age: 30 };
const { name, age } = person;
// Array Destructuring
const numbers = [1, 2, 3];
const [first, second] = numbers;
//Explain syntax and use cases
//Benefits of destructuring