JavaScript Interview Questions and Answers (Basic to Intermediate)
1. What are the different data types present in JavaScript?
- Primitive: String, Number, BigInt, Boolean, undefined, Symbol, null
- Non-Primitive: Object (includes Arrays, Functions)
2. Difference between == and ===?
- == performs type coercion
- === checks both value and type
3. What is a closure?
- A closure is a function that retains access to its lexical scope even when executed outside that
scope.
4. What is hoisting?
- Hoisting is JavaScript's behavior of moving declarations to the top of the scope.
5. What are arrow functions?
- Arrow functions are a concise way to write functions and don't bind their own 'this'.
6. Difference between var, let, and const?
- var: Function scope, hoisted, can reassign/redeclare
- let: Block scope, not hoisted, can reassign, not redeclare
- const: Block scope, not hoisted, cannot reassign or redeclare
7. What is event bubbling?
- Events propagate from the target element up to the root.
8. What are promises?
- Promises represent the eventual result of an asynchronous operation.
9. Difference between null and undefined?
- undefined: Variable declared but not assigned
- null: Variable assigned with no value intentionally
10. Explain the 'this' keyword.
- Refers to the object it belongs to. Arrow functions inherit 'this' from their parent scope.
11. Synchronous vs Asynchronous?
- Synchronous: Executes line-by-line
- Asynchronous: Executes tasks in the background using callbacks/promises
12. Use of async and await?
- async makes a function return a promise
- await waits for the promise to resolve
13. What are template literals?
- Strings enclosed by backticks allowing embedded expressions using ${}
14. What is destructuring?
- Extracting values from arrays/objects into distinct variables.
15. Important Array Methods?
- map(), filter(), reduce(), forEach(), find(), some(), every(), includes()