Advertisement
Google Ad Slot: content-top
JS Interview Questions
1. What are the differences between var, let, and const in JavaScript?
var: Function-scoped, can be redeclared and updated, hoisted but not initialized.let: Block-scoped, can be updated but not redeclared in the same scope, hoisted but not initialized.const: Block-scoped, cannot be updated or redeclared, must be initialized during declaration.
2. Explain the event loop in JavaScript.
The event loop is a fundamental concept in JavaScript that allows asynchronous operations to be executed without blocking the main thread. JavaScript is single-threaded, meaning it executes code one line at a time. The event loop helps manage asynchronous tasks like:
- setTimeout, setInterval
- Promises (fetch, AJAX)
- DOM events (click, keypress)
- I/O operations (reading files, database queries)
Even though setTimeout is 0ms, it goes to Web APIs and runs after the synchronous code.
Promises run before setTimeout because microtasks (Promises) have higher priority than macrotasks (setTimeout).
Fetch works asynchronously, so JavaScript continues execution and logs "Other synchronous tasks..." before receiving API data.
3. What is the difference between == and === in JavaScript?
==(Abstract Equality): Checks only values, allowing type coercion (e.g.,5 == "5"istrue).===(Strict Equality): Checks both values and types (e.g.,5 === "5"isfalse).
4. How does this keyword work in JavaScript?
The value of this depends on the execution context:
- Global scope:
thisrefers to thewindowobject (in browsers). - Inside a method:
thisrefers to the object calling the method. - Inside a function:
thisrefers toundefinedin strict mode, elsewindow. - In arrow functions:
thisis lexically inherited from the surrounding scope. - With
bind(),call(),apply():thiscan be explicitly set.
Here, this refers to user.
this refers to the new object (p1).
Why? Arrow functions don’t bind this, they inherit it from their parent scope (which is window in this case).
Here, this refers to the element that triggered the event.
Explicit Binding: call, apply, bind
call()explicitly sets this to person.
Similar to call(), but apply() takes an array of arguments.
bind()returns a new function with this set to user.
5. What is the difference between call, apply, and bind?
call(obj, arg1, arg2, …)→ Calls function withthisset toobj, arguments passed separately.apply(obj, [arg1, arg2, …])→ Similar tocall(), but arguments are passed as an array.bind(obj)→ Returns a new function withthispermanently set toobj.
6. What is closure in JavaScript?
A closure is a function that retains access to its parent scope even after the parent function has finished executing.
Here, innerFunction still has access to count even after outerFunction has returned.
7. Explain the difference between synchronous and asynchronous JavaScript.
- Synchronous JavaScript: Executes code sequentially, blocking further execution until the current task is completed.
- Asynchronous JavaScript: Uses mechanisms like callbacks, promises, or async/await to execute tasks in the background, allowing non-blocking execution.
8. What is the difference between map(), filter(), and reduce()?
Answer:
map(): Transforms each array element and returns a new array.filter(): Returns a new array with elements that satisfy a condition.reduce(): Reduces the array to a single value (e.g., sum, product).
9. Explain async and await in JavaScript.
async and await simplify working with promises by making asynchronous code look synchronous.
asyncmakes a function return a promise.awaitpauses execution until the promise resolves.
10. What is the difference between deep copy and shallow copy in JavaScript?
- Shallow Copy: Copies only references for nested objects, not actual values.
- Deep Copy: Recursively copies all object properties.
11. How does JavaScript handle memory management and garbage collection?
JavaScript uses Garbage Collection (GC) to free up memory that is no longer accessible.
- Uses Reference Counting and Mark-and-Sweep Algorithm to identify unused objects.
- Objects with no references are automatically removed from memory.