All Flashcards

1
Q

Syntax to Create object.

A
var object = new Object();
var object = Object.create(null);
var object = {
     name: "Sudheer"
     age: 34
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is prototype chaining?

A

Prototype chaining is used to build new types of objects based on existing ones. It is similar to inheritance in a class based language.

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

What is the difference between Call, Apply and Bind?

A

Call: The call() method invokes a function with a given this value and arguments provided one by one

Apply: Invokes the function with a given this value and allows you to pass in arguments as an array
invite.apply(employee1, [“Hello”, “How are you?”]);

bind: returns a new function, allowing you to pass any number of arguments

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

What is JSON and its common operations?

A

JSON is a text-based data format following JavaScript object syntax.

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

What is the purpose of the array slice method?

A

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.

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

What is the purpose of the array splice method?

A

The splice() method is used either adds/removes items to/from an array, and then returns 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.

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

How do you compare Object and Map?

A

Objects are similar to Maps in that both let you set keys to values, retrieve those values, delete keys, and detect whether something is stored at a key. Due to this reason, Objects have been used as Maps historically. But there are important differences that make using a Map preferable in certain cases.

The keys of an Object are Strings and Symbols, whereas they can be any value for a Map, including functions, objects, and any primitive.
The keys in Map are ordered while keys added to Object are not. Thus, when iterating over it, a Map object returns keys in order of insertion.

You can get the size of a Map easily with the size property, while the number of properties in an Object must be determined manually.

A Map is an iterable and can thus be directly iterated, whereas iterating over an Object requires obtaining its keys in some fashion and iterating over them.

An Object has a prototype, so there are default keys in the map that could collide with your keys if you’re not careful. As of ES5 this can be bypassed by using map = Object.create(null), but this is seldom done.

A Map may perform better in scenarios involving frequent addition and removal of key pairs.

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

What are lambda or arrow functions?

A

An arrow function is a shorter syntax for a function expression and does not have its own this, arguments, super, or new.target. These functions are best suited for non-method functions, and they cannot be used as constructors.

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

What is a first class function?

A

In Javascript, functions are first class objects. First-class functions means when functions in that language are treated like any other variable.

For example, in such a language, a function can be passed as an argument to other functions, can be returned by another function and can be assigned as a value to a variable. For example, in the below example, handler functions assigned to a listener.

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

What is a first order function

A

First-order function is a function that doesn’t accept another function as an argument and doesn’t return a function as its return value.

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

What is a higher order function?

A

Higher-order function is a function that accepts another function as an argument or returns a function as a return value or both.

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

What is the currying function?

A

Currying is the process of taking a function with multiple arguments and turning it into a sequence of functions each with only a single argument. Currying is named after a mathematician Haskell Curry. By applying currying, a n-ary function turns it into a unary function.

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

Writing pure function.

A
// Impure
let numberArray = [];
const impureAddNumber = (number) => numberArray.push(number);
// Pure
const pureAddNumber = (number) => (argNumberArray) =>
  argNumberArray.concat([number]);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the purpose of the let keyword?

A

The let statement declares a block scope local variable. Hence the variables defined with let keyword are limited in scope to the block, statement, or expression on which it is used.

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

What is IIFE?

A
(function () {
  // logic here
})();

The primary reason to use an IIFE is to obtain data privacy because any variables declared within the IIFE cannot be accessed by the outside world.

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

What is IIFE?

A
(function () {
  // logic here
})();

The primary reason to use an IIFE is to obtain data privacy because any variables declared within the IIFE cannot be accessed by the outside world.

16
Q

How do you decode or encode a URL in JavaScript?

A
let encoded_uri = encodeURI(uri);
let decoded_uri = decodeURI(encoded_uri);
17
Q

What is memoization?

A
const memoizeAddition = () => {
  let cache = {};
  return (value) => {
    if (value in cache) {
      console.log("Fetching from cache");
      return cache[value]; // Here, cache.value cannot be used as property name starts with the number which is not a valid JavaScript  identifier. Hence, can only be accessed using the square bracket notation.
    } else {
      console.log("Calculating result");
      let result = value + 20;
      cache[value] = result;
      return result;
    }
  };
};
// returned function from memoizAddition
const addition = memoizAddition();
console.log(addition(20)); //output: 40 calculated
console.log(addition(20)); //output: 40 cached
18
Q

What is Hoisting?

A

Hoisting is a JavaScript mechanism where variables, function declarations and classes are moved to the top of their scope before code execution. Remember that JavaScript only hoists declarations, not initialisation. Let’s take a simple example of variable hoisting.

19
Q

What are classes in ES6?

A
class Bike {
  constructor(color, model) {
    this.color = color;
    this.model = model;
  }

getDetails() {
return this.model + “ bike has” + this.color + “ color”;
}
}

20
Q

What are closures?

A

function Welcome(name) {
var greetingInfo = function (message) {
console.log(message + “ “ + name);
};
return greetingInfo;
}
var myFunction = Welcome(“John”);
myFunction(“Welcome “); //Output: Welcome John
myFunction(“Hello Mr.”); //output: Hello Mr.John

21
Q

What is web storage?

A

Web storage is an API that provides a mechanism by which browsers can store key/value pairs locally within the user’s browser, in a much more intuitive fashion than using cookies. The web storage provides two mechanisms for storing data on the client.

Local storage: It stores data for current origin with no expiration date.
Session storage: It stores data for one session and the data is lost when the browser tab is closed.

22
Q

What is a Cookie?

A

A cookie is a piece of data that is stored on your computer to be accessed by your browser. Cookies are saved as key/value pairs. For example, you can create a cookie named username as below,

Cookies are used to remember information about the user profile(such as username). It basically involves two steps,

When a user visits a web page, the user profile can be stored in a cookie.
Next time the user visits the page, the cookie remembers the user profile.

document. cookie = “username=John; expires=Sat, 8 Jun 2019 12:00:00 UTC”;
document. cookie = “username=John; path=/services”;

23
Q

What is a promise?

A

Promises are used to handle asynchronous operations. They provide an alternative approach for callbacks by reducing the callback hell and writing the cleaner code.

A promise is an object that may produce a single value some time in the future with either a resolved value or a reason that it’s not resolved(for example, network error). It will be in one of the 3 possible states: fulfilled, rejected, or pending.

24
Q

What is promise.all?

A

Promise.all is a promise that takes an array of promises as an input (an iterable), and it gets resolved when all the promises get resolved or any one of them gets rejected. For example, the syntax of promise.all method is below,