What is memory leak? Give an example
Types of Common JavaScript Leaks
Accidental global variables
function foo(arg) {
bar = 'this is a hidden global variable';
}
var elements = {
button: document.getElementByld('button'),
image: document.getElementByld('image'),
text: document.getElementById('text')
};
function doStuff() {
image.src = 'http://some.url/image';
button.click();
console.log(text.innerHTML);
// Much more logic
}
function removeButton() {
// The button is a direct child of body.
document.body.removechild(document.getElementBy!d('button'));
// At this point, we still have a reference to a button in the global
// elements dictionary. In other words, the button element is still in
// memory and cannot be collected by the GC.
}
What is hoisting?
What is the Temporal Dead Zone?
let and const keywords, but not with var.let or const variable before its declaration (within its scope) causes a ReferenceError. The time span when that happens, between the creation of a variable’s binding and its declaration, is called the temporal dead zone.What is pooling?
What does asynchronous mean?
What is memoization?
Memoization is a programming technique that attempts to increase a function’s performance by caching its previously computed results. Each time a memoized function is called, its parameters are used to index the cache. If the data is present, then it can be returned, without executing the entire function. Otherwise the function is executed and then the result is added to the cache.
function memoize(func) {
const storage = {};
return function (val) {
if (storage[val]) {
console.log('In Storage');
return storage[val];
} else {
console.log('Computating');
storage[val] = func(vaL);
return storage[val];
}
};
}
What are data types in JavaScript?
string, number, boolean, null, undefined, and symbol.object, function, and array.What are primitive data types?
What is the difference between primitives vs objects?
What is coercion in JavaScript?
Do you know what Autoboxing in JS is?
Number, String, Boolean) when you try to access their properties or methods.What is the difference between == and === operators?
== compares values for equality after type coercion.=== compares values for equality without type coercion.What is undefined data type?
undefined is a primitive data type that represents the absence of a value or uninitialized variable.What is null value?
null is a primitive data type that represents the intentional absence of any value or a variable that has been explicitly set to no value.What does isNaN function do?
isNaN() function checks if a value is not a number. It returns true if the value is NaN, and false otherwise.What does NaN value mean?
NaN represents an unrepresentable or undefined value that typically results from invalid mathematical operations.What is the difference between null and undefined?
null is a value that represents the intentional absence of any object value.undefined is a value that represents uninitialized variables or missing properties.How do you copy properties from one object to another?
Object.assign(), the spread operator (...), or by iterating over the properties and manually copying them.What are classes in ES6?
Explain how prototypal inheritance works?
Prototypal inheritance means that objects can inherit properties and methods from other objects through their prototype chain. When you access a property or method on an object, JavaScript looks up the prototype chain to find it.

How do you assign default values to variables?
|| operator or the new nullish coalescing operator (??).What is the difference between let, const, and var?
var has function-level scope, meaning that a variable declared with var is accessible within the function it is declared in, or globally if it is declared outside of a function. var variables can be redeclared and reassigned.let and const have block-level scope, meaning that a variable declared with let or const is only accessible within the block it is declared in (e.g. within a loop or an if statement).let variables can be reassigned, but not redeclared, while const variables cannot be reassigned or redeclared.let and const variables are hoisted, only they are hoisted without a default initialization. This makes them inaccessible (as such variables are in a temporal dead zone). Variables declared with var, on the other hand, are hoisted with a default initialization of undefined.What are the differences between undeclared and undefined variables?
var, let, or const. An undefined variable has been declared but has not been assigned a value.What are global variables?
What is Hoisting?
What is scope in JavaScript?
What is a closure?
How does closure implemented in JavaScript?
What is the difference between Call, Apply, and Bind?
call and apply are used to invoke functions with a specific this context and arguments. bind is used to create a new function with a specific this context but doesn’t immediately invoke it.What does ‘this loses context’ mean?
this inside a function is not what you expect. It often occurs when functions are passed as callbacks or methods are detached from their objects.What is JSON and its common operations?
JSON.parse() and converting JavaScript objects to JSON strings using JSON.stringify().How do you parse a JSON string?
JSON.parse() method.What is the purpose of JSON.stringify?
JSON.stringify() is used to convert a JavaScript object into a JSON-formatted string, making it suitable for data exchange or storage.What array methods do you know?
push(), pop(), shift(), unshift(), concat(), slice(), splice(), sort();forEach(), includes(), some(), each();map(), filter(), reduce().What is the difference between Array.forEach() and Array.map()?
forEach() iterates over an array and executes a provided function for each element. It returns undefined.map() also iterates over an array but creates a new array by applying a provided function to each element.What are lambda or arrow functions?
this value from their enclosing scope.What is a pure function?
What is IIFE (Immediately Invoked Function Expression)?
What is a callback function?
What is a promise? Why do we need a promise?
List all states of a promise?
pending, fulfilled, or rejected. Once a promise transitions to either fulfilled or rejected, it becomes settled, and its state cannot change.Why do we need callbacks?
What is a callback hell?
What is promise chaining?
What is the purpose of using setTimeout?
setTimeout() is used to schedule the execution of a function after a specified delay in milliseconds. It’s often used for delaying tasks, animations, or simulating asynchronous behavior.What is the purpose of using setInterval?
setInterval() is used to repeatedly execute a function at specified intervals until it’s cleared. It’s commonly used for tasks that need to be performed regularly, like updating a clock.What is an event loop?
The event loop is a mechanism in JavaScript that allows the runtime to perform non-blocking I/O operations despite the fact that JavaScript is single-threaded. It is responsible for handling asynchronous callbacks and executing them in the appropriate order.
When an asynchronous operation is initiated, it is added to a queue of pending events. The event loop continuously checks this queue for events, and when it finds one, it executes the associated callback function. This process continues until the queue is empty.
What is the call stack?
How do you validate an email in JavaScript?
/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/ or 3rd party libraries.What are modules? Why do you need modules?
What is a rest operator?
...) allows to gather the remaining arguments of a function into an array. It’s often used in function parameters to handle variable-length argument lists.What is a spread operator?
...) is used to spread the elements of an array or object into another array or object. It’s often used for creating copies or merging arrays/objects.What is Node.js?
What is the difference between window and document?
window is the top-level object in the browser’s JavaScript object hierarchy, and it represents the browser window or tab that the script is running in. It provides access to various properties and methods related to the browser window, such as window.innerWidth and window.location.document is a property of the window object and represents the current web page loaded in the browser. It provides access to the HTML content and allows manipulation of the DOM (Document Object Model) For example, you can use document.getElementById() to get a reference to an HTML element by its ID, and then manipulate its properties or contents..slice vs splice
The slice() method returns the selected elements in an array as a new array object. It selects the elements starting at the given start argument and ends at the given optional end argument without including the last element. If you omit the second argument then it selects till the end.
The splice() method is used to either adds/remove items to/from an array and then return the removed item. The first argument specifies the array position for insertion or deletion whereas the optional second argument indicates the number of elements to be deleted. Each additional argument is added to the array.
Note: The splice method modifies the original array and returns the deleted array.
What are lambda or arrow functions?
What is a first-class function?
What is a callback function?
What is a pure function?
What is a higher-order function?
Event.target vs event.currentTarget
event.target and event.currentTarget are properties commonly used in JavaScript when handling events. They refer to different elements involved in an event and can often be confused.
event.target: This property refers to the element that triggered the event. It identifies the most specific element that initially caused the event. For instance, if you have a click event on a button inside a div, clicking the button will make the button element the event.target.
event.currentTarget: This property refers to the element that the event handler is currently attached to. It does not change as the event bubbles up or propagates through the DOM (Document Object Model). It always remains the element to which the event handler is bound.
The difference can be more evident when dealing with nested elements or event delegation. Event delegation is the practice of using a single event listener to manage multiple elements. In this context, event.currentTarget remains constant (representing the element the event is delegated to), while event.target can vary based on the actual target that triggered the event within the delegated element.
Here’s an example to illustrate this difference:
<div id="outer">
<button id="inner">Click me</button>
</div>
document.getElementById('outer').addEventListener('click', function (event) {
console.log('Current Target: ' + event.currentTarget.id);
console.log('Target: ' + event.target.id);
});
In this case, clicking the button with the ID “inner” will log:
Current Target: outer (because the event listener is attached to the “outer” div)Target: inner (since the button triggered the event)Understanding these properties is crucial for handling events in JavaScript, especially when dealing with complex DOM structures or implementing event delegation.
stopPropagation() vs stopImmediatePropagation()
Both stopPropagation() and stopImmediatePropagation() are methods used in JavaScript to manage event propagation in the DOM. They affect how events bubble or propagate through the DOM hierarchy.
stopPropagation(): This method is used within an event handler to prevent the further propagation of the current event in the capturing and bubbling phases. When stopPropagation() is called on an event, it halts the event from propagating up the DOM tree, but it doesn’t stop the event from being executed on the current target. Other event handlers on the same element will still be executed.
element.addEventListener('click', function (event) {
event.stopPropagation();
// Other click handlers on the same element will still be triggered
});
stopImmediatePropagation(): This method, when called within an event handler, stops the event from propagating immediately and prevents any other handlers on the same element from being executed. It not only prevents the event from reaching the elements further up the DOM tree, but also stops other event handlers on the same element from running.
element.addEventListener('click', function (event) {
event.stopImmediatePropagation();
// Other click handlers on the same element will not be triggered
});
Understanding these methods is crucial when handling events in a complex DOM structure and wanting to control how events propagate through the elements.
Differents between load and DOMContentLoaded?
The load and DOMContentLoaded events are both related to the loading and initialization of a web page in the browser, but they differ in terms of when they are triggered and what they signify.
DOMContentLoaded event:
document.addEventListener('DOMContentLoaded', function () {
// This code runs when the DOM is fully loaded and ready for manipulation
});
load event:
window.addEventListener('load', function () {
// This code runs when the entire page and its resources (images, styles, etc.) are loaded
});