JavaScript fundamentals form-end and increasingly server-side development. At the intermediate level, interviews focus on deep conceptual understanding rather than just topics are commonly tested at companies like Amazon, Spotify, and mid-sized tech firms where developers need to demonstrate mastery of mechanics to solve complex.
Key Points:
Example:
function outer() { let count = 0; return function inner() { // Closure captures 'count' count++; return count; }; } const increment = outer(); console.log(increment()); // 1 console.log(increment());
Definition: : The mechanism that enables JavaScript (single-threaded) to handle asynchronous operations using a call stack and callback queue.
Key Points:
Example:
console.log('1'); setTimeout(() => console.log('2'), 0); Promise.resolve().then(() => console.log('3')); console.log('4'); // Output: 1, 4, 3, 2
Definition: : Mechanism where objects can inherit properties/methods from other objects through a prototype chain.
Key Points:
Object.create() for manual inheritanceclass syntax is syntactic sugar over prototypesExample:
function Animal(name) { this.name = name; } Animal.prototype.speak = function() { console.log(`${this.name} makes a noise.`); }; function Dog(name) { Animal.call(this, name); // Inherit properties } Dog.prototype = Object.create(Animal.prototype); Dog.prototype.constructor = Dog; const d = new Dog('Rex'); d.speak(); // Rex makes a noise.
Definition: : Different approaches to handling non-blocking operations in JavaScript.
| Pattern | Use Case | Key Advantage |
|---|---|---|
| Callbacks | Simple async operations | Direct control flow |
| Promises | Chaining async steps | Error propagation |
| async/await | Complex async flows | Synchronous-looking code |
Example:
// async/await example async function fetchData() { try { const response = await fetch('https://api.example.com/data'); const data = await response.json(); return data; } catch (error) { console.error('Error:', error); throw error; } }
Explain the difference between == and === in JavaScript
== performs type coercion before comparison=== checks both value and type without coercion=== to avoid unexpected behaviorconsole.log(0 == false); // true (coerced to 0 vs 0) console.log(0 === false); // false (different types)
Implement a debounce function
function debounce(fn, delay) { let timeoutId; return function(...args) { clearTimeout(timeoutId); timeoutId = setTimeout(() => fn.apply(this, args), delay); }; }
What is the purpose of the this keyword?
this in callbacks without bindingCompare and contrast forEach, map, and filter array methods
forEach: Executes callback for each element (returns undefined)map: Creates new array by transforming each elementfilter: Creates new array with elements that pass a testExplain IIFEs (Immediately Invoked Function Expressions)
(function() { const secret = 'top secret'; })();
console.log(a); // undefined (not ReferenceError due to hoisting) var a = 10; function checkHoisting() { console.log(b); // ReferenceError: b is not defined let b = 20; }
Explanation:
var declarations are hoisted to the top of their scope as undefinedlet declarations are hoisted but not initialized, causing Temporal Dead Zonefetch('invalid-url') .then(res => res.json()) .then(data => { console.log(data); return data.user.id; }) .then(userId => fetch(`/users/${userId}`)) .then(res => res.json()) .catch(err => console.error('Failed:', err));
Explanation:
.catch().then() receives the value from the previous handler.catch() at the end of chainsEasy: Convert this callback-based code to use promises
function readFile(path, cb) { fs.readFile(path, 'utf8', cb); }
> Use the 'util.promisify' module or wrap in a new Promise
Medium: Implement a throttle function that executes a function at most once every X milliseconds
> Track last execution time and use setTimeout/clearTimeout
Intermediate: Create a deep clone function that handles nested objects, arrays, and basic types without using JSON methods
> Use recursion and handle circular references carefully
=== and let/const: Avoid coercion pitfalls and accidental redeclarationthis context in callbacksvar vs letStudy Tips:
Start a new session to explore different topics or increase the difficulty level.
Start New Session