JavaScript Interview Questions
JavaScript is everywhere! Whether you're building interactive websites, dynamic web apps, or even backend services, mastering JavaScript is an essential skill for any developer.
Why Learn JavaScript? 🤔
JavaScript is the backbone of the web. With it, you can:
- Make web pages interactive 🎨
- Build full-fledged applications 🏗️
- Work with modern frameworks like React, Vue, and Angular ⚛️
- Even create mobile apps and server-side applications 📱🖥️
Beginner-Level Questions
Basics
- What is JavaScript?
- What are the key features of JavaScript?
- What are the differences between JavaScript and Java?
- How do you include JavaScript in an HTML file?
- What are the different data types in JavaScript?
- What are the different data types present in JavaScript?
- What is the difference between =and = =?
- Difference between =and = = operators.
- What is the difference between null and undefined?
- What is the use of the typeof operator in JavaScript?
- What are JavaScript variables? How do you declare them?
- What is the difference between let, const, and var?
- Differences between declaring variables using var, let, and const.
- What is hoisting in JavaScript?
- Explain Hoisting in JavaScript.
- Explain the difference in hoisting between var, let, and const.
- How does hoisting affect function declarations and expressions?
- What are the potential issues caused by hoisting?
- How can you avoid problems related to hoisting?
- What is scope in JavaScript?
- Explain Scope and Scope Chain in JavaScript.
- What is the difference between global and local scope?
- What is an arrow function? How is it different from a regular function?
- What are arrow functions?
- What is a closure in JavaScript?
- Explain Closures in JavaScript.
- What is an IIFE (Immediately Invoked Function Expression)?
- What is an Immediately Invoked Function in JavaScript?
- What is the this keyword in JavaScript?
- Explain the this keyword.
- What is JSON? How do you parse and stringify JSON in JavaScript?
- What are JavaScript promises?
- What is the use of promises in JavaScript?
- What is the difference between call, apply, and bind?
- Explain call(), apply(), and bind() methods.
- What is the difference between exec() and test() methods in JavaScript?
- What is currying in JavaScript?
- What is the rest parameter and spread operator?
- What is Implicit Type Coercion in JavaScript?
- Is JavaScript a statically typed or a dynamically typed language?
- What is the NaN property in JavaScript?
- Explain passed by value and passed by reference.
- Is JavaScript a pass-by-reference or pass-by-value language?
- What do you mean by strict mode in JavaScript and characteristics of JavaScript strict mode?
- What are some advantages of using External JavaScript?
- Mention some advantages of JavaScript.
- What is the use of a constructor function in JavaScript?
- What is recursion in a programming language?
- What is memoization?
- What is the Temporal Dead Zone?
DOM Manipulation
- What is the DOM (Document Object Model)?
- How do you select elements in the DOM using JavaScript?
- What is the difference between innerHTML and innerText?
- What is event bubbling and event capturing?
- What is the difference between addEventListener and onclick?
- Which method is used to retrieve a character from a certain index?
- What do you mean by BOM (Browser Object Model)?
- What is the distinction between client-side and server-side JavaScript?
- Describe the difference between a cookie, sessionStorage, and localStorage in browsers.
- Describe the difference between , , and .
- Explain event delegation in JavaScript.
- What is the difference between mouseenter and mouseover events in JavaScript and browsers?
- What is the difference between event.preventDefault() and event.stopPropagation()?
- How do you prevent the default behavior of an event?
- What is the difference between document.querySelector() and document.getElementById()?
- How do you add, remove, and modify HTML elements using JavaScript?
- What are event listeners and how are they used?
- Explain the event phases in a browser.
- What is the difference between innerHTML and textContent?
- How do you manipulate CSS styles using JavaScript?
Functions
- What is a first-class function?
- What is a higher-order function?
- Explain Higher Order Functions in JavaScript.
- What is a callback function?
- Why do we use callbacks?
- What is a pure function?
- What is a currying function?
- What is a thunk function?
- What are asynchronous thunks?
- What is a decorator in JavaScript?
- What is a proper tail call in JavaScript?
- What's a typical use case for anonymous functions in JavaScript?
- What is recursion and how is it used in JavaScript?
- What are default parameters and how are they used?
- Explain why the following doesn't work as an IIFE: function foo(){}();. What needs to be changed to properly make it an IIFE?
- What are the various ways to create objects in JavaScript?
- Explain the difference between dot notation and bracket notation for accessing object properties.
- What are the different methods for iterating over an array?
- How do you add, remove, and update elements in an array?
- What are the different ways to copy an object or an array?
- Explain the difference between shallow copy and deep copy.
- What are the advantages of using the spread operator with arrays and objects?
- How do you check if an object has a specific property?
- Explain the concept of destructuring assignment for objects and arrays.
- What is Object.freeze() for?
- What is Object.seal() for?
- What is Object.preventExtensions() for?
Arrays and Objects
- What is the purpose of the Array.prototype.slice method?
- What is the purpose of the Array.prototype.splice method?
- What is the difference between slice and splice?
- How do you compare Object and Map?
- What is the difference between Object.keys and Object.getOwnPropertyNames?
- What are object prototypes?
- What is the prototype design pattern?
- In JavaScript, how many different methods can you make an object?
- What is Object Destructuring?
- What are Sets and Maps and how are they used?
- What are the differences between Map/Set and WeakMap/WeakSet in JavaScript?
- How do you convert a Set to an array in JavaScript?
- What is the difference between a Map object and a plain object in JavaScript?
- How do Sets and Maps handle equality checks for objects?
Intermediate-Level Questions
Advanced Concepts
- What is the prototype chain in JavaScript?
- What is the difference between prototypal inheritance and classical inheritance?
- Difference between prototypal and classical inheritance.
- What is the difference between Object.create and the new keyword?
- What is the difference between Object.freeze and Object.seal?
- What is the difference between deep copy and shallow copy?
- What is the role of deferred scripts in JavaScript?
- What has to be done in order to put Lexical Scoping into practice?
- Explain the concept of lexical scoping.
- How can closures be used to create private variables?
- What are the potential pitfalls of using closures?
- Explain the difference between global scope, function scope, and block scope.
- Explain how this works in JavaScript.
- Explain the different ways the this keyword can be bound.
- What are the common pitfalls of using the this keyword?
- Explain the concept of this binding in event handlers.
- What is the DOM and how is it structured?
- What's the difference between an "attribute" and a "property" in the DOM?
- How do you add, remove, and modify HTML elements using JavaScript?
- What are event listeners and how are they used?
Asynchronous JavaScript
- What is the event loop in JavaScript runtimes?
- Explain the difference between synchronous and asynchronous functions in JavaScript.
- What are Promises and how do they work?
- Explain the different states of a Promise.
- What are the pros and cons of using Promises instead of callbacks in JavaScript?
- What is the use of Promise.all()?
- How is Promise.all() different from Promise.allSettled()?
- What is async/await and how does it simplify asynchronous code?
- How do you handle errors in asynchronous operations?
- Explain the concept of a microtask queue.
- What is the difference between setTimeout(), setImmediate(), and process.nextTick()?
- Explain how prototypal inheritance works in JavaScript.
- What is the prototype chain and how does it work?
- Explain the difference between classical inheritance and prototypal inheritance.
- Explain the concept of inheritance in ES2015 classes.
- What is the purpose of the new keyword?
- How do you create a constructor function?
- What are the differences between JavaScript ES2015 classes and ES5 function constructors?
- What advantage is there for using the JavaScript arrow syntax for a method in a constructor?
- Why might you want to create static class members in JavaScript?
ES6+ Features
- What are template literals in JavaScript?
- What is destructuring assignment in JavaScript?
- What are default parameters in JavaScript?
- What are JavaScript modules?
- What are JavaScript generators?
- What are generator functions?
- What are classes in JavaScript?
- What are Symbols used for in JavaScript?
- What are proxies in JavaScript used for?
- What are iterators and generators in JavaScript and what are they used for?
- Explain the difference between mutable and immutable objects in JavaScript.
- What is the difference between a Map object and a plain object in JavaScript?
- What are the differences between Map/Set and WeakMap/WeakSet in JavaScript?
- Why might you want to create static class members in JavaScript?
- What are JavaScript object getters and setters for?
- What are JavaScript object property flags and descriptors?
- How do you reliably determine whether an object is empty?
- What are the benefits of using a module bundler?
- Explain the concept of tree shaking in module bundling.
- What are the metadata fields of a module?
Error Handling
- What is the purpose of the Error object in JavaScript?
- What are the different types of errors in JavaScript?
- How do you handle errors using try...catch blocks?
- What is the purpose of the finally block?
- How can you create custom error objects?
- Explain the concept of error propagation in JavaScript.
- What are some best practices for handling sensitive data in JavaScript?
- Explain the concept of Content Security Policy (CSP) and how it enhances security.
- What are some common security headers and their purpose?
- How can you prevent clickjacking attacks?
- Explain the concept of input validation and its importance in security.
- What are some tools and techniques for identifying security vulnerabilities in JavaScript code?
- How can you implement secure authentication and authorization in JavaScript applications?
- Explain the same-origin policy with regards to JavaScript.
- What is use strict in JavaScript for?
- What tools and techniques do you use for debugging JavaScript code?
- How does JavaScript garbage collection work?
- Explain what a single-page app is and how to make it SEO-friendly.
- How can you share code between JavaScript files?
- How do you organize your code?
Advanced-Level Questions
Performance and Optimization
- What is tree shaking in JavaScript?
- What is the need for tree shaking?
- How do you optimize JavaScript performance?
- What is the difference between debounce and throttle?
- What is the purpose of the requestAnimationFrame method?
- What are some common performance bottlenecks in JavaScript applications?
- Explain the concept of debouncing and throttling.
- How can you optimize DOM manipulation for better performance?
- What are some techniques for reducing reflows and repaints?
- Explain the concept of lazy loading and how it can improve performance.
- What are Web Workers and how can they be used to improve performance?
- Explain the concept of caching and how it can be used to improve performance.
- What are some tools that can be used to measure and analyze JavaScript performance?
- How can you optimize network requests for better performance?
- What are the different types of testing in software development?
- Explain the difference between unit testing, integration testing, and end-to-end testing.
- What are some popular JavaScript testing frameworks?
- How do you write unit tests for JavaScript code?
- Explain the concept of test-driven development (TDD).
- What are mocks and stubs and how are they used in testing?
- How can you test asynchronous code in JavaScript?
- What are some best practices for writing maintainable and effective tests in JavaScript?
- Explain the concept of code coverage and how it can be used to assess test quality.
- What are some tools that can be used for JavaScript testing?
- What are design patterns and why are they useful?
- Explain the concept of the Singleton pattern.
- What is the Factory pattern and how is it used?
- Explain the Observer pattern and its use cases.
- What is the Module pattern and how does it help with encapsulation?
- Explain the concept of the Prototype pattern.
- What is the Decorator pattern and how is it used?
- Explain the concept of the Strategy pattern.
- What is the Command pattern and how is it used?
- Why is extending built-in JavaScript objects not a good idea?
- What is Cross-Site Scripting (XSS) and how can you prevent it?
- Explain the concept of Cross-Site Request Forgery (CSRF) and its mitigation techniques.
- How can you prevent SQL injection vulnerabilities in JavaScript applications?
- What are some best practices for handling sensitive data in JavaScript?
- Explain the concept of Content Security Policy (CSP) and how it enhances security.
- What are some common security headers and their purpose?
- How can you prevent clickjacking attacks?
- Explain the concept of input validation and its importance in security.
- What are some tools and techniques for identifying security vulnerabilities in JavaScript code?
- How can you implement secure authentication and authorization in JavaScript applications?
- Explain the same-origin policy with regards to JavaScript.
- What is use strict in JavaScript for?
- What tools and techniques do you use for debugging JavaScript code?
- How does JavaScript garbage collection work?
- Explain what a single-page app is and how to make it SEO-friendly.
- How can you share code between JavaScript files?
- How do you organize your code?
- What are some of the advantages/disadvantages of writing JavaScript code in a language that compiles to JavaScript?
- When would you use document.write()?
Coding Challenges
Beginner
- Write a function to reverse a string.
- Write a function to check if a string is a palindrome.
- Write a function to find the factorial of a number.
- Write a function to find the Fibonacci sequence up to a given number.
- Write a function to check if a number is prime.
Intermediate
- Write a function to flatten a nested array.
- Write a function to implement a debounce function.
- Write a function to implement a throttle function.
- Write a function to implement a deep clone of an object.
- Write a function to implement a memoization function.
Advanced
- Write a function to implement a polyfill for Array.prototype.map.
- Write a function to implement a polyfill for Array.prototype.filter.
- Write a function to implement a polyfill for Array.prototype.reduce.
- Write a function to implement a polyfill for Promise.all.
- Write a function to implement a polyfill for Promise.race.
Behavioral and Conceptual Questions
- How do you debug JavaScript code?
- Why do we use the word “debugger” in JavaScript?
- What tools do you use for debugging JavaScript?
- What are some best practices for writing clean JavaScript code?
- How do you handle cross-browser compatibility issues?
- What are some common security issues in JavaScript?