Skip to content

Commit c20e62c

Browse files
ananya8606sadanandpai
authored andcommitted
Update faq_js.mdx
1 parent 830be83 commit c20e62c

File tree

1 file changed

+156
-0
lines changed

1 file changed

+156
-0
lines changed

web/src/pages/interview_questions/faq_js.mdx

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,3 +300,159 @@
300300
console.error('An error occurred:', error.message);
301301
}
302302
```
303+
304+
### 21. What are template literals in JavaScript? Provide an example.
305+
*Answer:*
306+
- Template literals are a feature introduced in ES6 that allow for easier string interpolation and multi-line strings. They are enclosed by backticks (`) instead of single or double quotes.
307+
308+
```javascript
309+
const name = 'Alice';
310+
const greeting = `Hello, ${name}!`;
311+
console.log(greeting); // Output: Hello, Alice!
312+
```
313+
314+
### 22. Explain the concept of "prototypal inheritance" in JavaScript.
315+
*Answer:*
316+
- Prototypal inheritance is a feature in JavaScript where objects can inherit properties and methods from other objects. This is achieved through the prototype chain.
317+
318+
```javascript
319+
const animal = {
320+
speak() {
321+
console.log('Animal speaks');
322+
},
323+
};
324+
325+
const dog = Object.create(animal);
326+
dog.speak(); // Output: Animal speaks
327+
```
328+
329+
### 23. What are Promise.all() and Promise.race()? Provide examples of their usage.
330+
*Answer:*
331+
- Promise.all() takes an array of promises and returns a single promise that resolves when all of the promises in the array have resolved, or rejects if any promise is rejected.
332+
- Promise.race() returns a promise that resolves or rejects as soon as one of the promises in the array resolves or rejects.
333+
334+
```javascript
335+
// Example of Promise.all
336+
Promise.all([
337+
Promise.resolve(1),
338+
Promise.resolve(2),
339+
Promise.resolve(3),
340+
]).then(values => {
341+
console.log(values); // Output: [1, 2, 3]
342+
});
343+
344+
// Example of Promise.race
345+
Promise.race([
346+
new Promise((resolve) => setTimeout(() => resolve('First'), 1000)),
347+
new Promise((resolve) => setTimeout(() => resolve('Second'), 500)),
348+
]).then(result => {
349+
console.log(result); // Output: 'Second'
350+
});
351+
```
352+
353+
### 24. What are higher-order functions in JavaScript? Provide an example.
354+
*Answer:*
355+
- Higher-order functions are functions that take other functions as arguments or return functions as their result. This allows for more abstract and reusable code.
356+
357+
```javascript
358+
function createMultiplier(multiplier) {
359+
return function(x) {
360+
return x * multiplier;
361+
};
362+
}
363+
364+
const double = createMultiplier(2);
365+
console.log(double(5)); // Output: 10
366+
```
367+
368+
### 25. What is the difference between shallow copy and deep copy?
369+
*Answer:*
370+
- A shallow copy duplicates the top-level properties of an object. If the properties are references to other objects, the references are copied, not the actual objects.
371+
- A deep copy creates a new object and recursively copies all properties and their values.
372+
373+
```javascript
374+
const original = { a: 1, b: { c: 2 } };
375+
376+
// Shallow copy
377+
const shallowCopy = Object.assign({}, original);
378+
shallowCopy.b.c = 3; // Changes 'c' in both objects
379+
console.log(original.b.c); // Output: 3
380+
381+
// Deep copy
382+
const deepCopy = JSON.parse(JSON.stringify(original));
383+
deepCopy.b.c = 4; // Changes 'c' only in deepCopy
384+
console.log(original.b.c); // Output: 3
385+
```
386+
387+
### 26. What is the purpose of setTimeout and setInterval in JavaScript? Provide examples.
388+
*Answer:*
389+
- setTimeout is used to execute a function after a specified delay (in milliseconds).
390+
- setInterval is used to repeatedly execute a function at specified intervals.
391+
392+
```javascript
393+
setTimeout(() => {
394+
console.log('Executed after 1 second');
395+
}, 1000);
396+
397+
let count = 0;
398+
const intervalId = setInterval(() => {
399+
console.log('Count:', count++);
400+
if (count === 5) clearInterval(intervalId); // Stop after 5 counts
401+
}, 1000);
402+
```
403+
404+
### 27. What are the JavaScript data types?
405+
*Answer:*
406+
- JavaScript has several data types, including:
407+
408+
- Primitive Types:
409+
410+
1. string: Represents text values.
411+
2. number: Represents numeric values.
412+
3. boolean: Represents true or false.
413+
4. null: Represents an intentional absence of any object value.
414+
5. undefined: Represents a variable that has been declared but not yet assigned a value.
415+
6. symbol: Represents a unique identifier (introduced in ES6).
416+
7. bigint: Represents integers with arbitrary precision (introduced in ES11).
417+
418+
- Reference Types:
419+
420+
1. object: Represents complex data structures like arrays and objects.
421+
422+
423+
### 28. What is the difference between synchronous and asynchronous programming in JavaScript?
424+
*Answer:*
425+
- Synchronous programming executes code in a sequential manner, where each operation must complete before the next one starts.
426+
- Asynchronous programming allows for operations to be executed independently of the main program flow, enabling code to run in the background while other operations continue.
427+
428+
```javascript
429+
// Synchronous example
430+
console.log('First');
431+
console.log('Second');
432+
433+
// Asynchronous example
434+
setTimeout(() => {
435+
console.log('Asynchronous operation');
436+
}, 1000);
437+
console.log('Third');
438+
```
439+
440+
### 29. How can you check if a variable is an array in JavaScript?
441+
*Answer:*
442+
- You can use the Array.isArray() method to check if a variable is an array.
443+
444+
```javascript
445+
const arr = [1, 2, 3];
446+
console.log(Array.isArray(arr)); // Output: true
447+
448+
const obj = {};
449+
console.log(Array.isArray(obj)); // Output: false
450+
```
451+
452+
### 30. What is the difference between static and dynamic typing in JavaScript?
453+
*Answer:*
454+
- Static typing requires variable types to be declared explicitly, enabling type checking at compile time (not supported in
455+
JavaScript).
456+
- Dynamic typing allows variables to hold values of any type without explicit type declaration, enabling type checking at runtime.
457+
458+

0 commit comments

Comments
 (0)