JavaScript Practice Interview Questions (Part 2)

Welcome back to our series on JavaScript interview questions! This second part delves into more nuanced and advanced theoretical concepts that often come up in discussions with experienced JavaScript developers.
1. Explain call(), apply(), and bind() methods.
These three methods are used to manipulate the this context of a function and/or pass arguments.
call(thisArg, arg1, arg2, …): Invokes a function with a specified this value and individual arguments passed one by one.
apply(thisArg, [argsArray]): Invokes a function with a specified this value and arguments provided as an array (or an array-like object).
bind(thisArg, arg1, arg2, …): Returns a new function that, when called, has its this keyword set to the provided thisArg and pre-filled arguments. It does not immediately invoke the function.
2. What is prototypal inheritance in JavaScript?
JavaScript is a prototype-based language, meaning objects inherit properties and methods from other objects via a prototype chain. Every JavaScript object has an internal property called [[Prototype]] (accessible via __proto__ or Object.getPrototypeOf()) that links to its prototype object. When you try to access a property on an object, if it’s not found on the object itself, JavaScript looks up the prototype chain until it finds the property or reaches the end of the chain (null). This is how objects inherit behavior.
3. What is a Higher-Order Function (HOF)?
A Higher-Order Function is a function that either:
Takes one or more functions as arguments (e.g., map, filter, reduce).
Returns a function as its result (e.g., a function factory or a closure that returns another function). HOFs are a cornerstone of functional programming in JavaScript, enabling more abstract and reusable code.
4. Differentiate between var, let, and const keywords.
var:
Function-scoped: Variables declared with var are accessible throughout the entire function they are declared in.
Hoisted: Declarations are hoisted to the top of their function scope and initialized with undefined.
Can be re-declared and re-assigned.
let:
Block-scoped: Variables are confined to the block ({}) they are declared in.
Not hoisted to the top of the block in a usable sense (Temporal Dead Zone).
Can be re-assigned but not re-declared within the same scope.
const:
Block-scoped.
Not hoisted in a usable sense (Temporal Dead Zone).
Cannot be re-assigned or re-declared after initialization. However, if a const variable holds an object or array, the contents of that object/array can still be modified.
5. What are Promises and how do they solve callback hell?
Promises are objects representing the eventual completion or failure of an asynchronous operation and its resulting value. They provide a more structured and manageable way to handle asynchronous code compared to nested callbacks. Instead of deeply nested callbacks (“callback hell”), Promises allow you to chain .then() methods for sequential asynchronous operations and use a single .catch() block for error handling throughout the chain. This improves readability and maintainability.
6. Explain Strict Mode in JavaScript.
Strict Mode (‘use strict’;) is a feature in JavaScript that allows you to write “strict” code. It helps in writing more secure and robust JavaScript by:
Eliminating some JavaScript silent errors by changing them to throw errors.
Fixing mistakes that make it difficult for JavaScript engines to perform optimizations.
Prohibiting some syntax likely to be defined in future versions of ECMAScript.
‘use strict’; can be applied to an entire script or to individual functions.
7. What is event bubbling and event capturing?
These describe the order in which events are handled when multiple elements are nested within each other and an event occurs on one of them:
Event Bubbling: The default behavior. When an event occurs on an element, it first triggers on that element, then “bubbles up” to its direct parent, then to its parent’s parent, and so on, up to the document object.
Event Capturing (Trickling): The opposite of bubbling. The event first triggers on the outermost element (document), then “captures” down through the DOM tree to the target element. You can enable capturing by passing true as the third argument to addEventListener().
8. What is a JavaScript module? Why are they used?
JavaScript modules (introduced in ES6) are self-contained pieces of code that can export functions, objects, or primitive values from one file and import them into another. They help organize code into smaller, reusable units, promoting better maintainability, reusability, and preventing global scope pollution. Modules allow for clear dependencies and make it easier to manage large codebases.
9. Describe the concept of “Pass by Value” and “Pass by Reference” in JavaScript.
Pass by Value (Primitives): When you pass a primitive data type (like a number, string, or boolean) to a function, a copy of that value is created and passed. Any changes made to the parameter inside the function do not affect the original variable outside the function.
Pass by Reference (Objects/Non-Primitives): When you pass a non-primitive data type (like an object or array) to a function, a copy of the reference to that object in memory is passed. This means both the original variable and the function parameter point to the same object. Therefore, changes made to the object inside the function will affect the original object outside the function. However, reassigning the parameter itself to a new object inside the function will not affect the original variable.
10. What is NaN in JavaScript? How do you check for it?
NaN stands for “Not-a-Number.” It’s a special numeric value that represents an undefined or unrepresentable numerical result (e.g., 0 / 0, Math.sqrt(-1)).
It’s unique because it’s the only value in JavaScript that is not equal to itself (NaN === NaN is false).
To reliably check if a value is NaN, you should use the global isNaN() function or, preferably, Number.isNaN() (which is more robust as it doesn’t coerce its argument to a number before checking).
Originally published on Medium.