JavaScript Interview Questions
1. What is JavaScript?
JavaScript is a high-level, interpreted programming language used to make web pages interactive. It runs in the browser and can manipulate HTML, CSS, and respond to user events.
2. How do you declare a variable in JavaScript?
Using var, let, or const.
let name = 'John';
3. What are the data types in JavaScript?
- Primitive: String, Number, Boolean, null, undefined, Symbol, BigInt
- Non-primitive: Object, Array, Function
4. What is the difference between == and ===?
- == compares values with type coercion
- === compares both value and type strictly
5. What is a function in JavaScript?
A function is a block of reusable code.
function greet(name) {
return `Hello, ${name}`;
}
6. What is an array in JavaScript?
An array is a list-like object used to store multiple values.
let fruits = ['apple', 'banana', 'orange'];
7. What is the DOM?
DOM (Document Object Model) is the structure of HTML as objects that JavaScript can manipulate.
8. How do you add a comment in JavaScript?
- Single-line: // Comment
- Multi-line: /* Comment */
9. What is undefined in JavaScript?
A variable that has been declared but not assigned a value is undefined.
10. What is the purpose of typeof?
It returns the data type of a variable.
typeof 5 // "number"
11. What are arrow functions?
A shorthand for defining functions.
const add = (a, b) => a + b;
12. What is the difference between let, const, and var?
- var: function-scoped, can be redeclared
- let: block-scoped, can be reassigned
- const: block-scoped, cannot be reassigned
13. What are template literals?
A way to embed expressions in strings using backticks.
let name = 'John';
let msg = `Hello, ${name}`;
14. What is hoisting in JavaScript?
JavaScript moves declarations to the top of the scope. var declarations and function declarations are hoisted.
15. What is event delegation?
A technique where a single event listener handles events on multiple child elements by leveraging event bubbling.
16. What is the difference between null and undefined?
- undefined: variable declared but not assigned
- null: intentional absence of any value
17. How do you handle asynchronous code in JavaScript?
Using callbacks, Promises, or async/await.
18. What is a closure?
A function that retains access to its lexical scope even when the function is executed outside that scope.
function outer() {
let count = 0;
return function inner() {
return ++count;
}
}
19. What are higher-order functions?
Functions that take other functions as arguments or return them.
[1, 2, 3].map(x => x * 2);
20. What is the difference between call(), apply(), and bind()?
- call: calls a function with given this and arguments
- apply: same as call but takes arguments as an array
- bind: returns a new function with this bound
21. What is the event loop in JavaScript?
It handles asynchronous callbacks. JavaScript runs synchronously, but the event loop manages callback queues and executes them when the call stack is clear.
22. What are Promises?
Objects representing eventual completion or failure of an asynchronous operation.
new Promise((resolve, reject) => {
resolve('Success');
});
23. What is async/await?
Syntactic sugar over Promises for writing asynchronous code that looks synchronous.
async function getData() {
let data = await fetch(url);
}
24. What is prototypal inheritance?
Objects can inherit properties and methods from other objects via their prototype chain.
25. What is the this keyword?
Refers to the object from which the function was called. In arrow functions, it refers to the enclosing context.
26. What are modules in JavaScript?
Modular code files that export/import features.
export function add() {}
import { add } from './math.js';
27. What is a debounce function?
A function that delays execution until after a wait time has elapsed since the last call. Useful in limiting API calls or resize events.
28. What is the difference between synchronous and asynchronous code?
- Synchronous code blocks further execution until complete
- Asynchronous code allows the program to continue running
29. What is memory leak in JavaScript?
When memory that is no longer needed is not released. Common causes: global variables, detached DOM nodes, closures holding references.
30. Explain the concept of currying in JavaScript.
Transforming a function with multiple arguments into a sequence of functions each taking one argument.
function curry(a) {
return function(b) {
return a + b;
}
}