Interview prep Flashcards
(39 cards)
Which of the following is not true of JS 1. JS is case sensitive language 2. Semicolons at the end of JS statements are required 3. JS statements can be grouped together in blocks. 4. Javascript is loosely typed
- is false. JS is sensitive to the case of the function name, class name, identifiers, etc. JS is a loosely typed language - meaning you don’t have to specify what type of information will be stored in a variable in advance. For example, we use int, string, etc to initialize a variable but here we can give let, const, etc for initializing.
What should be called before send() in XMLHttpRequest
Open() Example: var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { // Action to be performed when the document is read; } }; xhttp.open(“GET”, “filename”, true); xhttp.send();
(function(){ console.log(1); setTimeOut(()=>{console.log(2)},1000); setTimeOut(()=>{console.log(3)},0); console.log(4) })();
1 4 3 2
Hoisting
Hoisting #
In JavaScript, a variable can be declared after it has been used. This is because variable declarations using var are hoisted to the top of their functional scope at compile time. Hence, a variable can be initialized and used before it has been declared.

Can you offer a use case for the new arrow => function syntax?
- For array manupulation
eg : map, reduce, forEach etc
- Managing async code
ex: resolving promise and using then as chaining

What is lexical scoping.
In JavaScript, arrow functions do not bind their own this, meaning they inherit the one from the parent scope; this is also known as lexical scoping.
What is the result of the below:
<style></style>
button {font-size: 50px; }
.on {background: #ff0000;}
<button>Click me</button>
const button = document.querySelector(‘#pushy’);
button. addEventListener(‘click’, () => {
this. classList.toggle(‘on’);
});
The keyword this does not bind to the element clicked as it is being used inside the arrow function. Due to this, the error, “TypeError, cannot read property, toggle of undefined”, occurs.
call & apply

what does Function.prototype.bind do?
The bind function creates a new function whose this value can be set to the value provided during the function call, enabling the calling of a function with a specified this value (the first parameter to bind function).

What does Function.call do?
The call method allows for a method that was defined for one object to be assigned and called on by another object. This allows for a method to get defined once and then get inherited by other objects without having to re-write it for other objects.

Difference between call and apply
The only difference between the two is that call expects all parameters to be passed individually, whereas apply expects the second argument to be an array of all the parameters.
what are closure and common uses of closure
In JavaScript, a closure is a function that can access the variables from another function’s scope. This is possible by creating nested functions. The enclosing/outer function will, however, not have access to the inner scope.
The inner function having access to the variables in the outer function. It is not a true visa versa
Used for below:
- Data object privacy
- In event handlers
- callback functions
- currying

What are event table, event loop and event queue
Event table & queue #
As you know, the call stack stores all your running JavaScript code. The interpreter reads the code line-by-line, pushes each statement into the call stack one-by-one, and pops them once they execute. However, if the statement is asynchronous, it is removed from the call stack and forwarded to the event table. This table is responsible for moving the asynchronous code to the event queue after a specified time. Here the statement waits for execution.
This brings us to the question. When will the statements from the event queue execute?
The event loop is responsible for keeping check of both the call stack and the event queue. It keeps checking if all the statements from the call stack have finished execution; that is, if the call stack is empty. If so, it pops the statement from the event queue (if present) to the call stack to execute.
Note: All synchronous statements execute first, and only then can the asynchronous ones execute.

What is IIFE
IIFE #
IIFE stands for Immediately Invoked Function Expressions. As the name implies, it is a way to execute the functions as soon as they are created.
- Annonymous
(function() {
//code goes here
})()
What does rest operator do?

What does spread operator do?

Primitive datatypes in JS
primitives (boolean, string, function, number, null, undefined)
What happens when we create object using new?
Usually, this is what new does:
- Creates a new object.
- Points this to the newly created object.
- Sets the prototype of the new object to the constructor function’s prototype.
- Makes the constructor function return this object if it is not returning anything else.
Note: return with an object returns the object. return with a primitive type is ignored, and this is returned instead.
“what is an event loop?
The event loop has the task of checking both the call stack and the task queue continuously. When it sees that the call stack is done executing all the commands and is empty, it pops the commands from the task queue and pushes them into the call stack for execution.
CALL STATCK AND EVENT QUEUE
The purpose of a call stack is to keep track of all the function calls. Whenever we invoke a function, it is pushed into the call stack first. Once it finishes execution, it gets popped. However, if an asynchronous command enters the call stack, it is popped and added into the task queue, also known as the event queue. Such asynchronous commands include Web Apis callbacks. When the call stack encounters them, it pops them and they enter the task queue instead. The call stack processes these commands once all other commands finish execution, and it is empty.
Describe event bubbling in JavaScript.
In event bubbling, the handler first executes on the event attached to the target element, then on all its ancestors. It starts from the bottom (deepest layer) and goes to the top.
Describe event capturing in JavaScript.
In event capturing, when you click the button, the event passes from the parent down to the event target,

Which of the following methods can prevent event bubbling?
event. stopPropagation()
event. cancelBubble
What are native objects.
In JavaScript, native objects are built-in objects. These are pre-defined objects that are always available regardless of the environment as long as JavaScript is running. Some common ones include:
String()
Boolean()
Number()
Array()
Object()
Function()
Date()
RegExp()
Error()
Image()








