25 Advanced JavaScript Questions
25 Advanced JavaScript Questions
JavaScript
Questions
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
1
19. Question: How does the event loop work in JavaScript? 10
20. Question: Explain the concept of arrow functions in JavaScript. 10
21. Question: What is the purpose of the Object.create() method in JavaScript?
10
22. Question: Explain the concept of the event bubbling and capturing phases.11
23. Question: What is a RESTful API, and how does it work in JavaScript? 11
24. Question: What is the purpose of the setTimeout function in JavaScript? 12
25. Question: How does the typeof operator work in JavaScript? 13
Dynamically typed.
Supports both procedural and object-oriented programming.
Runs on the client side (web browsers).
Asynchronous programming with callbacks and Promises.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
2
let: Block-scoped, can be reassigned, and is hoisted.
const: Block-scoped, cannot be reassigned after declaration, and is hoisted.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
3
6. Question: Describe the concept of prototypal inheritance in
JavaScript.
Answer:
JavaScript uses prototypal inheritance, where objects can inherit properties and
methods from other objects via a prototype chain. Each object has a prototype
object, and if a property is not found in the object, JavaScript looks up the chain
until it finds the property or reaches the end.
function outer() {
let outerVar = 10;
function inner() {
console.log(outerVar);
}
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
4
return inner;
}
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
5
10. Question: What is the difference between == and === in
JavaScript?
Answer:
==: Loose equality operator, only checks for equality of values after type coercion.
===: Strict equality operator, checks for equality of values and types without type
coercion.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
6
async is used to declare an asynchronous function, and await is used to pause the
execution of an async function until the promise is resolved, returning the
resolved value.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
7
const numbers = [1, 2, 3, 4];
const sum = numbers.reduce((acc, num) => acc + num, 0);
// Result: 10
// Storing data
localStorage.setItem('username', 'John');
// Retrieving data
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
8
const username = localStorage.getItem('username');
console.log(username); // Outputs: John
try {
// Code that might throw an error
throw new Error('An error occurred');
} catch (error) {
console.error(error.message);
} finally {
// Code that always runs
}
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
9
19. Question: How does the event loop work in JavaScript?
Answer:
The event loop is a mechanism in JavaScript that allows the execution of code to
be non-blocking. It consists of a call stack, callback queue, and event loop. The call
stack processes synchronous code, while asynchronous code is handled through
callback functions pushed to the callback queue by web APIs.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
10
const person = {
greet: function() {
console.log('Hello!');
}
};
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
11
DELETE) for communication and is stateless. In JavaScript, the fetch API is often
used to interact with RESTful APIs.
Start
End
Delayed
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
12
25. Question: How does the typeof operator work in
JavaScript?
Answer:
The typeof operator returns a string indicating the type of an operand. It is often
used to check the type of a variable.
console.log(typeof 42); // Outputs: 'number'
console.log(typeof 'Hello'); // Outputs: 'string'
console.log(typeof true); // Outputs: 'boolean'
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
13