50 Javascript Interview Questions Part 1
50 Javascript Interview Questions Part 1
The == (equality) and === (strict equality) operators are used for
comparison in JavaScript.
== (Equality): Performs type coercion before comparison and then it
compares values after converting them to a common type.
=== (Strict Equality): Does not perform type coercion and it compares
both value and type
3. What is the difference between null and undefined?
the declaration of x is hoisted to the top, but not its initialization. That's why the first
`console.log outputs undefined
4. Explain the concept of hoisting in JavaScript.
4. Explain the concept of hoisting in JavaScript.
let and const declarations are hoisted but not initialized. This leads to a
"temporal dead zone" where accessing the variable before its declaration
results in a ReferenceError
5. What is the difference between let, const, and var?
Variable scope refers to the context in which a variable is declared and can be
accessed. In JavaScript, there are two main types of scope:
1. Global Scope: Variables declared outside any function or block
2. Local Scope: Variables declared inside a function or block
JavaScript uses lexical scoping, which means that inner functions have access to
variables in their outer scope.
7. Explain the difference between global and local variables.
Global Variables: Declared outside any function or block. Accessible from anywhere in the
code, including inside functions. Have global scope
Local Variables: Declared inside a function or block. Only accessible within that
function or block. Have local scope
8. What is the temporal dead zone?
The Temporal Dead Zone (TDZ) is the period between entering a scope and
the point where a variable is declared and initialized.
Variable shadowing occurs when a variable declared in a certain scope has the
same name as a variable in an outer scope. The inner variable "shadows" the
outer one, effectively hiding it.
10. What is a closure in JavaScript?
A closure in JavaScript is a function that has access to variables in its outer (enclosing)
lexical scope, even after the outer function has returned. Closures are created every
time a function is created
10. What is a closure in JavaScript?
A pure function is a function that: Always returns the same output for the
same inputs. Has no side effects (doesn't modify external state). Doesn't rely
on external state. Make code more predictable and easier to test.
15. Difference between function declaration and function expression?
Hoisting: Function declarations are hoisted, function expressions are not. Usage:
Function declarations can be called before they appear in the code, function
expressions cannot.
Naming: Function declarations require a name, function expressions can be
anonymous.
16. What is an Immediately Invoked Function Expression (IIFE)?
The `this` keyword refers to the object that is executing the current function. Its
value is determined by how a function is called.
21. What are the different ways to loop through an array in JavaScript?
21. What are the different ways to loop through an array in JavaScript?
22. Explain the difference between for...in and for...of loops.
23. How do you add/remove elements from an array?
24. What is the purpose of the map() function?
The map() method creates a new array with the results of calling a provided
function on every element in the array.
25. Explain the difference between filter() and find() methods.
26. Explain the difference between some() and every() methods.
some() method: Returns true if at least one element in the array satisfies the provided
testing function. Stops iterating as soon as it finds an element that satisfies the condition.
Returns false if no elements satisfy the condition.
every() method: Returns true if all elements in the array satisfy the provided testing
function. Stops iterating as soon as it finds an element that doesn't satisfy the
condition.
27. How do you select elements in the DOM using JavaScript?
28. How do you create and append elements to the DOM?
29. Explain the difference between innerHTML and textContent.
Key differences:
1. innerHTML parses content as HTML, textContent does not
2. innerHTML can be slower and less secure (risk of XSS attacks)
3. textContent is generally faster and safer
4. innerHTML returns all content, including script and style element content
5. textContent returns the text content of all elements, ignoring tags
30. How do you remove an element from the DOM?
31. What are arrow functions and how do they differ from regular functions?
32. Explain the concept of destructuring in JavaScript.
Template literals are string literals that allow embedded expressions and
multi-line strings.
34. What is spread operator, Explain with example?
Callbacks are functions passed as arguments to other functions, often used for
asynchronous operations. Callback hell occurs when multiple nested callbacks make code
hard to read and maintain.
38. What is a Promise in JavaScript with example?
Promise.all() takes an iterable of promises and returns a single Promise that resolves
when all input promises have resolved, or rejects if any input promise rejects.
41. What is the purpose of the finally() method in Promises?
The finally() method is used to specify code that should be executed regardless of
whether the promise is fulfilled or rejected.
42. What is the purpose of the async await ?
The purpose of async/await is to simplify the syntax for working with Promises,
making asynchronous code easier to write and read. It allows you to write
asynchronous code that looks and behaves more like synchronous code.
43. How do you handle errors in async/await?
44. What is the difference between async/await and Promises?
A module can have multiple named exports but only one default export.
Named exports are imported using curly braces, while default exports are imported
without them.
Default exports can be imported with any name, while named exports must be
imported with their exact names (unless renamed using `as`).
46. How do you convert a JavaScript object to a JSON string?
47. How do you parse a JSON string back into a JavaScript object?
48. What is localStorage in JavaScript, and how do you store and retrieve data from it?
localStorage is a web storage object that allows you to store key-value pairs in the
browser with no expiration time
49. What is the difference between localStorage and sessionStorage?
Both localStorage and sessionStorage are web storage objects, but they differ in data
persistence.
localStorage data persists even after the browser is closed and reopened.
sessionStorage data is cleared when the page session ends (i.e., when the tab is
closed).
Both have the same API and are limited to storing string data.
50. How do you delete a specific item from localStorage or clear all data from it?