50 JavaScript Interview Questions
50 JavaScript Interview Questions
1. What is JavaScript?
var has function scope, `let` has block scope, and `const` is used to
declare constants whose value cannot be reassigned.
`==` is used for loose equality comparison, whereas `===` is used for strict
equality comparison, which checks both value and type.
Intermediate Level
11. Explain closures in JavaScript.
Closures are functions that have access to their outer function's scope, even
after the outer function has finished executing.
The `this` keyword refers to the object on which a method is being invoked or
the context in which a function is called.
`null` is an explicitly assigned value that represents the absence of any object
value, `undefined` indicates a variable that has been declared but has not yet
been assigned a value, and `undeclared` refers to variables that have not
been declared at all.
25. What are JavaScript modules and how do they improve code organization?
JavaScript modules are reusable pieces of code that encapsulate related
functionality and are designed to promote modularity, encapsulation, and code
reuse, leading to better code organization and maintainability
Advanced Level
26. What are generators in JavaScript?
Generators are functions that can be paused and resumed, allowing for more
flexible control flow.
30. What are closures and how are they implemented in JavaScript?
Closures are functions that have access to the outer function's variables even
after the outer function has finished executing. They are implemented by creating
a scope chain that remains intact even after the outer function exits.
Modules are reusable pieces of code that encapsulate related functionality. They
help in organizing code, managing dependencies, and promoting reusability.
`null` is an explicitly assigned value that represents the absence of any object
value, while `undefined` indicates a variable that has been declared but has
not yet been assigned a value.
35. Explain the concept of `bind, call`, and `apply` methods in JavaScript.
`bind`, `call`, and `apply` are methods used to manipulate the this
keyword in JavaScript functions. `bind` creates a new function with a specified
this value, call calls a function with a specified `this` value and arguments
passed individually, and `apply` calls a function with a specified `this` value
and arguments passed as an array.
Function currying is the process of transforming a function that takes multiple arguments into a
series of functions that each take a single argument. For example:
function multiply(a) {
return function(b) {
return a * b;
};
}
37. What are closures and how can they lead to memory leaks in JavaScript?
Closures are functions that have access to their outer function's scope even after
the outer function has finished executing. They can lead to memory leaks if they
hold references to large objects or variables that are no longer needed,
preventing them from being garbage collected.
38. Explain the concept of prototypal inheritance and how it differs from classical
inheritance.
39. What are the differences between `==` and `===` in JavaScript? Provide examples.
`==` performs type coercion before comparison, while` ===` does not. For example:
40. Explain the difference between the `for...in` and `for...of` loops in JavaScript.
The `for...in` loop iterates over the enumerable properties of an object, while the
`for...of` loop iterates over iterable objects such as arrays, strings, maps, and sets,
returning their property values.