Javascript Fundamentals Interview Questions
Comprehensive javascript fundamentals interview questions and answers for Javascript. Prepare for your next job interview with expert guidance.
Questions Overview
1. What is the difference between var, let, and const declarations in JavaScript?
Basic2. Explain type coercion in JavaScript and its implications.
Moderate3. What is hoisting in JavaScript and how does it affect variables and functions?
Basic4. How does the 'this' keyword work in different contexts?
Advanced5. What are primitive data types in JavaScript?
Basic6. How does prototypal inheritance work in JavaScript?
Advanced7. Explain closure in JavaScript and its use cases.
Advanced8. How does event bubbling and capturing work?
Moderate9. What is the event loop in JavaScript?
Advanced10. How do you properly compare values in JavaScript?
Basic11. What is scope chain in JavaScript?
Moderate12. How does garbage collection work in JavaScript?
Advanced13. Explain template literals and their features.
Basic14. What are the different ways to create objects in JavaScript?
Moderate15. How do you handle numeric precision in JavaScript?
Moderate16. What are Map and Set objects and their use cases?
Moderate17. How does strict mode affect JavaScript execution?
Moderate18. What are the different types of functions in JavaScript?
Basic19. How do you handle property descriptors and object immutability?
Advanced20. What are the differences between null and undefined?
Basic21. How do you implement proper error handling?
Moderate22. What are symbols and their use cases?
Advanced23. How do you handle decimal calculations accurately?
Moderate24. What is the module pattern and its implementation?
Advanced25. How do you implement property getters and setters?
Moderate26. What are WeakMap and WeakSet used for?
Advanced27. How do you handle type checking in JavaScript?
Basic28. What are the best practices for variable naming and declarations?
Basic29. How do you implement proper memory management?
Advanced1. What is the difference between var, let, and const declarations in JavaScript?
Basicvar has function scope, hoisted with undefined value. let and const have block scope, hoisted but not initialized (temporal dead zone). const prevents reassignment but doesn't make objects immutable. Consider scope implications, use const by default, let when reassignment needed.
2. Explain type coercion in JavaScript and its implications.
ModerateType coercion automatically converts values between types during operations. Implicit coercion occurs in comparisons (== vs ===), operations (+, -, etc.). Can lead to unexpected results. Consider explicit type conversion, use strict equality (===). Example: '5' + 2 results in '52' due to string concatenation.
3. What is hoisting in JavaScript and how does it affect variables and functions?
BasicHoisting moves declarations to top of scope during compilation. Function declarations fully hoisted with implementation. var declarations hoisted with undefined value. let/const hoisted without initialization. Consider declaration placement, understand scope rules.
4. How does the 'this' keyword work in different contexts?
Advanced'this' refers to current execution context. In methods, refers to object. In global scope, refers to window/global (non-strict) or undefined (strict). In event handlers, refers to event target. Use bind/arrow functions to maintain context. Consider lexical scope.
5. What are primitive data types in JavaScript?
BasicSeven primitive types: string, number, boolean, null, undefined, symbol, bigint. Immutable values, passed by value. Each has wrapper object (String, Number, etc.). Consider type checking, conversion methods. Understand differences from objects.
6. How does prototypal inheritance work in JavaScript?
AdvancedObjects inherit properties/methods through prototype chain. Each object has internal [[Prototype]] link. Properties looked up through chain until found or null reached. Use Object.create() or constructor functions. Consider performance, inheritance depth.
7. Explain closure in JavaScript and its use cases.
AdvancedClosure allows function to access variables from outer scope even after outer function returns. Used for data privacy, module pattern, partial application. Maintains state between function calls. Consider memory implications, avoid memory leaks.
8. How does event bubbling and capturing work?
ModerateEvents propagate through DOM tree. Capturing phase (top-down), target phase, bubbling phase (bottom-up). Control with addEventListener third parameter. Stop propagation with stopPropagation(). Consider event delegation, performance implications.
9. What is the event loop in JavaScript?
AdvancedEvent loop handles asynchronous operations. Manages call stack, callback queue, microtask queue. Executes synchronous code first, then microtasks, then macrotasks. Consider task priorities, understand single-threaded nature.
10. How do you properly compare values in JavaScript?
BasicUse === for strict equality (type and value). == performs type coercion. Object comparison checks reference. Use Object.is() for NaN, +0/-0. Consider value vs reference comparison. Handle null/undefined cases.
11. What is scope chain in JavaScript?
ModerateScope chain determines variable access. Inner scope can access outer scope variables. Created when function declared. Affects variable lookup performance. Consider lexical scope, closure implications. Handle naming conflicts.
12. How does garbage collection work in JavaScript?
AdvancedAutomatic memory management through reference counting and mark-and-sweep. Objects garbage collected when unreachable. Handle memory leaks (closures, event listeners). Consider weak references, cleanup patterns.
13. Explain template literals and their features.
BasicString literals allowing embedded expressions (${expression}). Support multiline strings, tagged templates. Enable string interpolation. Consider escaping, performance. Handle dynamic content generation.
14. What are the different ways to create objects in JavaScript?
ModerateObject literals, constructor functions, Object.create(), classes. Each method has different prototype behavior. Consider inheritance needs, performance. Handle property descriptors, initialization patterns.
15. How do you handle numeric precision in JavaScript?
ModerateUse Number.EPSILON for floating-point comparison. Handle IEEE 754 limitations. Consider BigInt for large integers. Use toFixed() for display. Implement proper rounding strategies.
16. What are Map and Set objects and their use cases?
ModerateMap allows any type as keys, maintains insertion order. Set stores unique values. Both provide efficient lookup. Consider WeakMap/WeakSet for memory. Handle iteration, conversion methods.
17. How does strict mode affect JavaScript execution?
ModerateStrict mode enables stricter parsing/error handling. Prevents implicit globals, this coercion. Requires variable declaration. Consider compatibility, module behavior. Handle legacy code integration.
18. What are the different types of functions in JavaScript?
BasicFunction declarations, expressions, arrow functions, generator functions. Each has different this binding, arguments handling. Consider hoisting behavior, performance implications. Handle scope appropriately.
19. How do you handle property descriptors and object immutability?
AdvancedUse Object.defineProperty(), Object.freeze(), Object.seal(). Control property behavior (writable, enumerable, configurable). Consider deep vs shallow immutability. Handle prototype chain.
20. What are the differences between null and undefined?
Basicundefined represents uninitialized value, null represents intentional absence. Different behavior in type coercion, comparisons. Consider type checking, default values. Handle both cases appropriately.
21. How do you implement proper error handling?
ModerateUse try-catch blocks, error objects, custom errors. Handle async errors with promises. Consider error types, propagation. Implement proper logging, recovery strategies.
22. What are symbols and their use cases?
AdvancedSymbols create unique identifiers. Used for private properties, special methods (Symbol.iterator). Non-enumerable by default. Consider well-known symbols, registration. Handle symbol description.
23. How do you handle decimal calculations accurately?
ModerateUse libraries (decimal.js), multiplication and division tricks. Handle IEEE 754 limitations. Consider precision requirements. Implement proper rounding. Handle display formatting.
24. What is the module pattern and its implementation?
AdvancedModule pattern encapsulates private data/methods. Uses closures, IIFE. Provides public interface. Consider import/export syntax, bundling. Handle dependency management.
25. How do you implement property getters and setters?
ModerateUse get/set keywords, Object.defineProperty(). Control property access/modification. Implement validation, computed properties. Consider performance implications. Handle inheritance.
26. What are WeakMap and WeakSet used for?
AdvancedWeakMap/WeakSet allow garbage collection of keys/values. Prevent memory leaks in certain patterns. No enumeration methods. Consider memory management, use cases. Handle cleanup properly.
27. How do you handle type checking in JavaScript?
BasicUse typeof, instanceof, Object.prototype.toString. Handle null/undefined cases. Consider type coercion rules. Implement proper validation. Handle edge cases appropriately.
28. What are the best practices for variable naming and declarations?
BasicUse meaningful names, proper casing (camelCase). Declare variables at appropriate scope. Consider hoisting, block scope. Use const by default. Handle naming conflicts.
29. How do you implement proper memory management?
AdvancedRemove event listeners, clear intervals/timeouts. Handle closure memory leaks. Consider weak references. Implement proper cleanup. Monitor memory usage. Handle large data structures.