JavaScript Practice Interview Questions (Part 1)

1. What is the difference between null and undefined in JavaScript?
undefined indicates that a variable has been declared but has not yet been assigned a value. It can also be the value of a function argument that wasn’t provided, or the return value of a function that doesn’t explicitly return anything.
null is an assignment value. It explicitly represents the intentional absence of any object value or “no value.” It’s a primitive value, and when checked with typeof, it returns “object” (which is a long-standing bug in JavaScript).
2. Explain JavaScript Hoisting.
Hoisting is a JavaScript mechanism where variable and function declarations are moved to the top of their containing scope during the compilation phase, before code execution. This means you can use variables and call functions before they are declared in your code. However, only the declarations are hoisted, not the initializations. For var variables, the declaration is hoisted but it’s initialized with undefined. let and const declarations are also hoisted, but they are in a “Temporal Dead Zone” (TDZ) until their actual declaration in the code is encountered, meaning you cannot access them before that point without getting a ReferenceError.
3. What is the difference between == and === operators?
The == (loose equality) operator compares two values after converting them to a common type (type coercion). This can sometimes lead to unexpected results (e.g., false == 0 is true).
The === (strict equality) operator compares both the value and the type without performing any type coercion. If the values are of different types, it immediately returns false. This is generally the recommended operator for comparisons in JavaScript.
4. What are JavaScript closures? Provide a simple explanation.
A closure is a function that remembers its lexical scope even when the function is executed outside that lexical scope. In simpler terms, an inner function has access to the variables and parameters of its outer (enclosing) function, even after the outer function has finished executing. This allows for data privacy and maintaining state.
5. Explain the this keyword in JavaScript.
The this keyword in JavaScript refers to the object that is executing the current function. Its value is determined dynamically at runtime and depends on how the function is called.
In a global context, this refers to the global object (e.g., window in browsers).
In a method call, this refers to the object the method belongs to.
In a function call (non-strict mode), this also refers to the global object. In strict mode, it’s undefined.
In a constructor function, this refers to the newly created instance.
With call(), apply(), or bind(), this can be explicitly set.
Arrow functions do not have their own this; they inherit this from their lexical (enclosing) scope.
6. What are Primitive and Non-Primitive data types in JavaScript?
Primitive Data Types: These store a single value and are immutable (their values cannot be changed after creation). They are passed by value. Examples include: string, number, boolean, null, undefined, symbol (ES6), and bigint (ES11).
Non-Primitive (Reference) Data Types: These store collections of data or more complex structures. They are mutable (their values can be changed) and are passed by reference. Examples include: object, array, and function.
7. What is the Event Loop in JavaScript?
The Event Loop is a fundamental concept for understanding JavaScript’s asynchronous behavior. JavaScript is single-threaded, meaning it can only execute one task at a time. The Event Loop works by continuously checking if the call stack is empty. If it is, it looks into the message queue (or task queue/callback queue) and moves the first message’s callback function to the call stack to be executed. This mechanism allows non-blocking I/O operations and handles asynchronous events like setTimeout, AJAX calls, and user interactions without freezing the main thread.
8. What is the Temporal Dead Zone (TDZ)?
The Temporal Dead Zone (TDZ) is a period between the creation of a let or const variable’s binding and its declaration in the code. During this period, attempting to access the variable will result in a ReferenceError. Unlike var, which would lead to undefined due to hoisting, let and const enforce stricter behavior, making it easier to catch potential errors related to variable access before declaration.
9. How does JavaScript handle asynchronous operations?
JavaScript handles asynchronous operations primarily through:
Callbacks: Functions passed as arguments to other functions, to be executed after the asynchronous operation completes. This can lead to “callback hell” or “pyramid of doom” with deeply nested callbacks.
Promises: Objects representing the eventual completion (or failure) of an asynchronous operation. They provide a more structured way to handle asynchronous code, allowing for chaining .then() and .catch() methods.
Async/Await (ES2017): Syntactic sugar built on top of Promises, making asynchronous code look and behave more like synchronous code. async functions always return a Promise, and the await keyword pauses the execution of an async function until a Promise settles (resolves or rejects).
10. What is event delegation in JavaScript?
Event delegation is a technique where you attach a single event listener to a parent element, instead of attaching individual event listeners to multiple child elements. When an event (like a click) occurs on a child element, it “bubbles up” to the parent. The single event listener on the parent can then identify the actual target of the event using event.target and perform actions accordingly. This improves performance by reducing the number of event listeners in the DOM, especially for dynamically added elements.
Originally published on Medium.