0% found this document useful (0 votes)
1K views

50 Javascript Interview Questions Part 1

Uploaded by

ayushsingh20406
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views

50 Javascript Interview Questions Part 1

Uploaded by

ayushsingh20406
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 55

1. What are the data types in JavaScript?

JavaScript has eight data types, divided into two categories:


primitive and non-primitive (object).
2. What is the difference between == and ===?

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?

Null and undefined are both used to represent the absence of a


value.
Null: Must be explicitly assigned. Typeof null returns "object" (this is a
known JavaScript bug)

Undefined: Represents a variable that has been declared but not


assigned a value .Typeof undefined returns "undefined"
4. Explain the concept of hoisting in JavaScript.

Hoisting is a behavior in JavaScript where variable and function declarations are


moved to the top of their respective scopes during the compilation phase, before
the code is executed. This means that regardless of where variables and functions are
declared in the code, they are treated as if they are declared at the beginning of
their scope.

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?

var: Function-scoped or globally-scoped. Can be redeclared and


updated and Hoisted and initialized with undefined.

let: Block-scoped. Hoisted but not initialized (Temporal Dead Zone).

const: Block-scoped. Cannot be updated or redeclared. Hoisted but not


initialized (Temporal Dead Zone)
6. What is variable scope in JavaScript?

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.

Applies to variables declared with let and const.


Helps catch errors by preventing access to variables before they're declared
9. What is variable shadowing?

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?

1.The createGreeter function returns an inner function that


forms a closure.
2.The inner function has access to the `greeting` parameter
and the `count` variable from its outer scope.
3.Each time we call createGreeter, it creates a new closure
with its own enclosed count variable.
4.The returned functions (casualGreeter and
formalGreeter) maintain their own separate counts,
demonstrating how closures preserve state.
11. What are the different ways to define a function in JavaScript?
12. What is a higher-order function?

A higher-order function is a function that treats other functions as data,


either by taking them as arguments or returning them.
13. Explain the concept of function hoisting.

Function hoisting is JavaScript's behavior of moving function declarations to


the top of their scope during the compilation phase. This allows you to call a
function before it appears to be defined in the code.
14. What is a pure function?

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)?

An IIFE is a JavaScript function that runs as soon as it is


defined.
17. How do you create an object in JavaScript?
18. How do you add/remove properties to an object dynamically?
19. How do you check if a property exists in an object?
20. What is the “this” keyword in JavaScript?

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.

Destructuring is a way to extract multiple values from data stored in


objects and arrays.
33. What are template literals?

Template literals are string literals that allow embedded expressions and
multi-line strings.
34. What is spread operator, Explain with example?

The spread operator (...) allows an iterable to be expanded in places


where zero or more arguments or elements are expected.
35. What are default parameters in ES6?

Default parameters allow you to set default values for function


parameters if no value or undefined is passed.
36. How do you use the rest parameter in functions?

The rest parameter syntax allows a function to accept an indefinite number of


arguments as an array.
37. What is callback & callback hell 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?

A Promise is an object representing the eventual completion or failure of


an asynchronous operation.
39. How do you chain Promises?

Promise chaining allows you to perform sequential asynchronous


operations.
40. What is the purpose of the Promise.all() method?

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?

Syntax: Async/await provides a more linear, synchronous-


looking code structure.

Error handling: Async/await uses try/catch, which is more


familiar for synchronous code.

Chaining: Promises use .then() for chaining, while


async/await uses regular JavaScript control flow.

Debugging: Async/await can be easier to debug as it behaves


more like synchronous code
45. What is the difference between default and named exports?

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?

You might also like