Interview prep Flashcards

1
Q

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

A
  1. 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What should be called before send() in XMLHttpRequest

A

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();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

(function(){ console.log(1); setTimeOut(()=>{console.log(2)},1000); setTimeOut(()=>{console.log(3)},0); console.log(4) })();

A

1 4 3 2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Hoisting

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Can you offer a use case for the new arrow => function syntax?

A
  1. For array manupulation

eg : map, reduce, forEach etc

  1. Managing async code
    ex: resolving promise and using then as chaining
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is lexical scoping.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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’);

});

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

call & apply

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

what does Function.prototype.bind do?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What does Function.call do?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Difference between call and apply

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

what are closure and common uses of closure

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are event table, event loop and event queue

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is IIFE

A

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.

  1. Annonymous

(function() {

//code goes here

})()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What does rest operator do?

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What does spread operator do?

A
17
Q

Primitive datatypes in JS

A

primitives (boolean, string, function, number, null, undefined)

18
Q

What happens when we create object using new?

A

Usually, this is what new does:

  1. Creates a new object.
  2. Points this to the newly created object.
  3. Sets the prototype of the new object to the constructor function’s prototype.
  4. 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.

19
Q

“what is an event loop?

A

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.

20
Q

CALL STATCK AND EVENT QUEUE

A

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.

21
Q

Describe event bubbling in JavaScript.

A

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.

22
Q

Describe event capturing in JavaScript.

A

In event capturing, when you click the button, the event passes from the parent down to the event target,

23
Q

Which of the following methods can prevent event bubbling?

A

event. stopPropagation()
event. cancelBubble

24
Q

What are native objects.

A

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

25
Q

What are Host objects

A

Host objects are provided to JavaScript by the execution environment, for instance, the web browser in the form of a document and window.

Ex: .table() and .error()

console is a host object provided to JavaScript. It has the method .log that developers use very often

26
Q

This

A

The JavaScript this keyword refers to the object it belongs to.

  1. The value of this is decided based on how the code is being executed.
  2. By default, the execution context for execution is global — which means if a code is being executed as part of a simple function call, then this refers to a global object.
  3. When a function is invoked with the new keyword, then the function is known as a constructor function and returns a new instance. In such cases, the value of this refers to a newly created instance.
  4. When a function is invoked as an object’s method, then this is the object that the method belong to
  5. If a function is invoked with call, bind, and apply methods, the value of this refers to the first argument passed.

In ES6 ‘this’ do not get bound to the arrow function. The value of this inside arrow function will be bound to the parent scope.

27
Q

Difference between classical and Prototypal inheritance

A

Classical inheritance - with extend keyword

Prototypal inheritance - with Object.create() or new keyword

28
Q

Prototypal inheritance

A

https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Inheritance

29
Q

Classical Inheritance

A

https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Inheritance

30
Q

Create a wrapper curry function.

For example: multiply(2,3,4) -> 24

mul(2)(3)(4) -> 24

A
31
Q

Same origin policy

A
32
Q
A
33
Q

Feature detection and feature inference and User agent?

A

User Agent - accesses information of the browser environment you are using.

When you visit a browser, it sends a user-agent string to the host; this string contains information about the browser environment you are navigating.

34
Q

Can HTML attributes be changed?

Can DOM properties be changed?

A

HTML attributes that can never be changed.

DOM properties can be modified

35
Q

difference between using innerHTML and appendChild.

A

document.getElementById(“list”).innerHTML = ‘

  • Names

Here we are adding a list item to another list. innerHTML takes the parent node and removes all its children; it replaces them with the node created by parsing the HTML in the given string

  • Names
    . Hence it causes a complete rebuild of the content of the parent node.

Append child

var list = document.getElementById(“list”) var elm = document.createElement(‘li’); elm.appendChild(document.createTextNode(‘Names’)); list.appendChild(elm)

While using appendChild we create a new element and insert it as a new node at the end of the target node; hence, it does not cause a complete rebuild of the DOM. It does not require any parsing of any HTML string either.

You can mention to the interviewer the situations in which they should be used — for example, using appendChild for appending elements and innerHTML where you need to replace the content of an element.

36
Q

The factors based on which you decide the third party library

A

The factors would be:
1. How active the third party library is. (Number of stars, number of commits, number of open issues)
2. How many contributors are there and how easy it is to contribute to the third party library.
3. Is the library open source and what is its adoption.
The way I would go about this is to lean on the following points.
1. Fit in our business logic.
2. Developer experience.
3. Ease of ramp up and change.

So my decision will be based on above factors. For example, if I am building a UI and we need a UI framework, I will zero in on React because it is time tested,popular and extremely easy to adopt. But if we are looking for calender component that is very specific to what the company does, a third party calender component might not be a correct choice.

37
Q

Callback

A

A callback is a function that is passed to another function. When the first function is done, it will run the second function.

The problem with callbacks is it creates something called “Callback Hell.” Basically, you start nesting functions within functions within functions, and it starts to get really hard to read the code.

38
Q

Promise , Async Await

A

A promise is an object that may produce a single value sometime in the future: either a resolved value or a reason that it’s not resolved (e.g., a network error occurred). A promise may be in one of 3 possible states: fulfilled, rejected, or pending.

39
Q

How many datatypes does JS has?

A

JavaScript has seven data types, Boolean, Null, Undefined, Number, String, Symbol, and Object