Basic JavaScript Questions
-
What are the different data types in JavaScript?
- Primitive types:
String
,Number
,Boolean
,Null
,Undefined
,Symbol
,BigInt
. - Non-primitive types:
Object
(includesArray
,Function
, etc.).
- Primitive types:
-
Explain the concept of
hoisting
in JavaScript.- Hoisting is JavaScript's default behavior of moving declarations to the top of their containing scope during the compile phase. Functions and variable declarations are hoisted.
-
What is the difference between
let
,const
, andvar
?var
is function-scoped and can be re-declared and updated.let
is block-scoped and can be updated but not re-declared within the same scope.const
is block-scoped and cannot be updated or re-declared; the value must be initialized during declaration.
-
How does the
this
keyword work in JavaScript?this
refers to the object that is currently calling the function. Its value depends on the context in which a function is called.
-
What are JavaScript closures and how are they used?
- A closure is a function that remembers and accesses variables from its lexical scope, even when the function is executed outside that scope. They are used for data privacy and creating function factories.
-
What is event delegation in JavaScript?
- Event delegation involves using a single event listener to manage events for multiple elements by taking advantage of event bubbling. It's efficient for managing events on dynamically added elements.
-
Explain the difference between
==
and===
operators.==
compares values for equality with type coercion.===
compares values for strict equality without type coercion.
-
What is the purpose of the
use strict
directive?use strict
enables strict mode, which helps catch common coding errors, prevents the use of unsafe features, and generally makes code safer and more optimized.
-
How do you create and manipulate arrays in JavaScript?
- Arrays can be created using array literals
[]
or theArray
constructor. Manipulation methods includepush
,pop
,shift
,unshift
,splice
,slice
,map
,filter
,reduce
, etc.
- Arrays can be created using array literals
-
What is the difference between
null
andundefined
?undefined
means a variable has been declared but not assigned a value.null
is an assignment value representing no value or an empty value.
Intermediate JavaScript Questions
-
How do you handle errors in JavaScript using
try
,catch
, andfinally
?try
block contains code that may throw an error.catch
block handles the error.finally
block contains code that executes regardless of whether an error occurred or not.
-
What are promises and how do you use them?
- A promise represents a value that may be available now, in the future, or never. Promises handle asynchronous operations. Methods:
then
,catch
,finally
.
- A promise represents a value that may be available now, in the future, or never. Promises handle asynchronous operations. Methods:
-
Explain the concept of async/await in JavaScript.
async/await
simplifies working with promises, making asynchronous code look and behave more like synchronous code.async
functions return promises, andawait
pauses execution until the promise resolves.
-
What is the Event Loop and how does it work?
- The event loop manages the execution of asynchronous code in JavaScript. It continuously checks the call stack and the callback queue, pushing callback functions to the call stack when it's empty.
-
Describe how the JavaScript garbage collection works.
- JavaScript uses a garbage collection mechanism to automatically manage memory. The most common algorithm is mark-and-sweep, which removes objects that are no longer reachable from the root.
-
What are arrow functions and how do they differ from regular functions?
- Arrow functions provide a shorter syntax for writing functions. They do not have their own
this
,arguments
,super
, ornew.target
and are always anonymous.
- Arrow functions provide a shorter syntax for writing functions. They do not have their own
-
How do you perform deep and shallow copying of objects in JavaScript?
- Shallow copy: Use
Object.assign
or the spread operator (...
). - Deep copy: Use libraries like Lodash (
_.cloneDeep
) or structured cloning withstructuredClone
.
- Shallow copy: Use
-
What are IIFEs (Immediately Invoked Function Expressions) and why are they used?
- IIFEs are functions that are executed immediately after being defined. They are used to create a local scope and avoid polluting the global scope.
-
What is the difference between localStorage, sessionStorage, and cookies in JavaScript?
localStorage
is used to store data with no expiration date.sessionStorage
is used to store data for the duration of the page session.- Cookies are used to store data that can be sent to and from the server with each HTTP request.
Advanced JavaScript Questions
-
How do you use modules in JavaScript, and what is the difference between CommonJS and ES6 modules?
- CommonJS: Used in Node.js, uses
require
andmodule.exports
. - ES6 modules: Use
import
andexport
, providing a standard module system for JavaScript.
- CommonJS: Used in Node.js, uses
-
What are the different ways to optimize JavaScript performance?
- Techniques include minimizing DOM access, using efficient algorithms, debouncing/throttling, lazy loading, minimizing reflows, and using web workers for heavy computations.
-
How do you manage state in modern JavaScript applications?
- State can be managed using various patterns and libraries such as Redux, MobX, Context API, and hooks in React.
-
Explain the concept of Higher-Order Functions in JavaScript.
- Higher-order functions are functions that take other functions as arguments or return functions. They enable functional programming techniques like map, filter, and reduce.
-
What is memoization in JavaScript, and how can you implement it?
- Memoization is an optimization technique used to speed up expensive function calls by caching the results of those calls. If the function is called again with the same arguments, the cached result is returned instead of recomputing.
-
What is the difference between synchronous and asynchronous programming in JavaScript?
- Synchronous programming executes code sequentially, blocking subsequent operations until the current one completes. Asynchronous programming, on the other hand, allows code to run in the background, enabling non-blocking operations and improving performance, especially for I/O-bound tasks.