Here’s a list of 50 common JavaScript questions along with their answers to help you get started or refresh your knowledge:
1. What is JavaScript?
JavaScript is a high-level, interpreted programming language primarily used for creating interactive effects within web browsers. It allows developers to implement complex features on web pages.
2. What are the data types supported by JavaScript?
JavaScript supports the following data types:
- Primitive: Number, String, Boolean, Undefined, Null, Symbol, BigInt
- Non-primitive: Object (including Arrays and Functions)
3. What is a closure in JavaScript?
A closure is a function that retains access to its lexical scope, even when the function is executed outside that scope. It allows for data encapsulation and privacy.
4. What is the difference between ==
and ===
?
==
is the equality operator that checks for value equality, performing type coercion if necessary. ===
is the strict equality operator that checks for both value and type equality.
5. Explain the concept of “hoisting.”
Hoisting is JavaScript’s behavior of moving declarations to the top of the current scope during compilation. Variables declared with var
are hoisted, while let
and const
are not.
6. What are promises in JavaScript?
Promises are objects that represent the eventual completion (or failure) of an asynchronous operation and its resulting value. They can be in one of three states: pending, fulfilled, or rejected.
7. What is an IIFE?
An Immediately Invoked Function Expression (IIFE) is a function that runs as soon as it is defined. It helps create a private scope.
8. What is the difference between null
and undefined
?
null
is an assignment value representing “no value,” while undefined
indicates that a variable has been declared but not assigned a value.
9. How does the this
keyword work?
The value of this
depends on how a function is called. In a method, this
refers to the object the method is called on; in a function, it defaults to the global object (or undefined
in strict mode).
10. What are template literals?
Template literals are string literals enclosed by backticks (`
) that allow for embedded expressions, multi-line strings, and string interpolation.
11. What is event delegation?
Event delegation is a technique that involves using a single event listener on a parent element to manage events for multiple child elements, improving performance and memory efficiency.
12. What are arrow functions?
Arrow functions are a concise syntax for writing function expressions. They do not have their own this
, making them ideal for methods that require access to the enclosing context.
13. What is the difference between let
, const
, and var
?
var
: Function-scoped, can be re-declared and updated.let
: Block-scoped, can be updated but not re-declared in the same scope.const
: Block-scoped, cannot be updated or re-declared; must be initialized.
14. What is the purpose of async/await
?
async/await
provides a way to work with promises more comfortably, allowing asynchronous code to be written in a synchronous style.
15. What is the Document Object Model (DOM)?
The DOM is a programming interface for web documents. It represents the structure of a document as a tree of objects and allows programming languages to manipulate the content, structure, and style of a webpage.
16. How can you create an object in JavaScript?
Objects can be created using:
- Object literals:
{ key: value }
- Constructor functions:
function Person() {}
Object.create()
:Object.create(proto)
- Classes:
class Person { constructor() {} }
17. What is the difference between forEach
and map
?
forEach
executes a function on each element of an array but does not return a new array, while map
creates and returns a new array with the results of calling a provided function on every element.
18. What is the spread operator?
The spread operator (...
) allows an iterable (like an array) to be expanded in places where zero or more arguments or elements are expected, making it useful for merging arrays or passing parameters.
19. What is the difference between synchronous and asynchronous code?
Synchronous code executes sequentially, blocking further execution until it completes. Asynchronous code allows operations to run independently, enabling other code to execute before the asynchronous operation finishes.
20. What are higher-order functions?
Higher-order functions are functions that take other functions as arguments or return functions as their result, allowing for powerful functional programming patterns.
21. What is a callback function?
A callback function is a function passed into another function as an argument, which is then invoked inside that function to complete some kind of routine or action.
22. What is the purpose of the bind
method?
The bind
method creates a new function that, when called, has its this
keyword set to a specified value, allowing for method borrowing and preserving context.
23. What is destructuring in JavaScript?
Destructuring is a syntax that allows unpacking values from arrays or properties from objects into distinct variables, simplifying code.
24. What is the difference between deep copy and shallow copy?
A shallow copy creates a new object but does not recursively copy nested objects, leading to shared references. A deep copy creates a completely independent copy of an object, including nested objects.
25. What is the purpose of setTimeout
and setInterval
?
setTimeout
executes a function after a specified delay, while setInterval
repeatedly executes a function at specified intervals.
26. What is the fetch
API?
The fetch
API provides a modern interface for making network requests, returning promises that resolve to the response of the request.
27. What is the difference between localStorage and sessionStorage?
localStorage
persists data across browser sessions, while sessionStorage
stores data for the duration of the page session, clearing when the tab is closed.
28. What are modules in JavaScript?
Modules are reusable pieces of code that can export and import functionality. They help in organizing code and managing dependencies.
29. What is the purpose of the new
keyword?
The new
keyword creates an instance of an object. It calls the constructor function, sets the prototype, and returns the new object.
30. What is JSON?
JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy for humans to read and write and easy for machines to parse and generate.
31. What is a function expression?
A function expression is a function defined within an expression rather than in a declaration. It can be anonymous or named and is often assigned to a variable.
32. What is the purpose of the console
object?
The console
object provides access to the browser’s debugging console, offering methods for logging information, warnings, and errors.
33. What is the instanceof
operator?
The instanceof
operator checks whether an object is an instance of a specific constructor or class, returning true or false.
34. What is the event loop in JavaScript?
The event loop is a mechanism that allows JavaScript to perform non-blocking I/O operations. It manages the execution of asynchronous code, processing events from the callback queue.
35. What is the typeof
operator?
The typeof
operator returns a string indicating the type of the unevaluated operand. It can return types such as “string,” “number,” “boolean,” “object,” “function,” and “undefined.”
36. What are the different ways to handle errors in JavaScript?
Errors can be handled using:
try/catch
statements- Custom error objects
- Promises with
.catch()
async/await
with try/catch
37. What is the difference between a function declaration and a function expression?
A function declaration defines a named function that is hoisted, while a function expression defines a function that is not hoisted and can be anonymous.
38. What is an event in JavaScript?
An event is an action or occurrence that happens in the browser (like clicks, keyboard input, etc.) that can be detected and responded to via JavaScript.
39. How can you prevent default behavior of an event?
The default behavior of an event can be prevented using the event.preventDefault()
method within an event handler.
40. What is the difference between an array and an object?
An array is a special type of object used for storing ordered collections of values, while an object is a collection of key-value pairs that may not have an order.
41. What is the window
object?
The window
object represents the browser’s window and serves as the global context for JavaScript in a web page