sudheerj/javascript-interview-questions Flashcards

1
Q

What are the possible ways to create objects in JavaScript

A
1. Object constructor:
       var object = new Object();
2. Object's create method:
       var object = Object.create(null);
3. Object literal syntax:
    var object = {
       name: "Sudheer"
       age: 34
    };
4. Function constructor:
function Person(name){
   this.name=name;
   this.age=21;
}
var object = new Person("Sudheer");
5. ES6 Class syntax:
  class Person {
   constructor(name) {
      this.name = name;
   }
}

var object = new Person(“Sudheer”);

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

What is a prototype chain

A

When it comes to inheritance, JavaScript only has one construct: objects. Each object has a private property which holds a link to another object called its prototype. That prototype object has a prototype of its own, and so on until an object is reached with null as its prototype. By definition, null has no prototype, and acts as the final link in this prototype chain.

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

They all attach this into function (or object) and the difference is in the function invocation (see below).

Call invokes the function and allows you to pass in arguments one by one.
Apply invokes the function and allows you to pass in arguments as an array.
Bind returns a new function, allowing you to pass in a this array and 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, which was popularized by Douglas Crockford. It is useful when you want to transmit data across a network and it is basically just a text file with an extension of .json, and a MIME type of application/json

It has Parsing: Converting a string to a native object

JSON.parse(text)

Stringification: converting a native object to a string so it can be transmitted across the network

JSON.stringify(object)

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.

Note: Slice method won’t mutate the original array but it returns the subset as a new array.

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.

Note: Splice method modifies the original array and returns the deleted array.

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

What is the difference between slice and splice

A

Slice:

  • Doesn’t modify the original array(immutable)
  • Returns the subset of original array
  • Used to pick the elements from array

Splice:

  • Modifies the original array(mutable)
  • Returns the deleted elements as array
  • Used to insert or delete elements to/from array
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
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
9
Q

What is the difference between == and === operators

A

JavaScript provides both strict(===, !==) and type-converting(==, !=) equality comparison. The strict operators take type of variable in consideration, while non-strict operators make type correction/conversion based upon values of variables. The strict operators follow the below conditions for different types,

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

What is a unary function

A

Unary function (i.e. monadic) is a function that accepts exactly one argument. It stands for a single argument accepted by a function.

const unaryFunction = a => console.log (a + 10);`

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
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.
Curried functions are great to improve code reusability and functional composition.

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

What is a pure function

A

A Pure function is a function where the return value is only determined by its arguments without any side effects. i.e, If you call a function with the same arguments ‘n’ number of times and ‘n’ number of places in the application then it will always return the same value.

Remember that Pure functions are important as they simplify unit testing without any side effects and no need for dependency injection. They also avoid tight coupling and make it harder to break your application by not having any side effects. These principles are coming together with Immutability concept of ES6 by giving preference to const over let usage.

17
Q

What is the difference between let and var

A

It is been available from the beginning of JavaScript|
Introduced as part of ES6
It has function scope It has block scope
Variables will be hoisted Hoisted but not initialized

18
Q

What is the reason to choose the name let as a keyword

A

let is a mathematical statement that was adopted by early programming languages like Scheme and Basic. It has been borrowed from dozens of other languages that use let already as a traditional keyword as close to var as possible.

19
Q

How do you redeclare variables in switch block without an error

A

If you try to redeclare variables in a switch block then it will cause errors because there is only one block. For example, the below code block throws a syntax error as below,

To avoid this error, you can create a nested block inside a case clause and create a new block scoped lexical environment.

let counter = 1;
    switch(x) {
      case 0: {
        let name;
        break;
      }
      case 1: {
        let name; // No SyntaxError for redeclaration.
        break;
      }
    }
20
Q

What is the Temporal Dead Zone

A

The Temporal Dead Zone is a behavior in JavaScript that occurs when declaring a variable with the let and const keywords, but not with var. In ECMAScript 6, accessing a 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.

21
Q

What is IIFE(Immediately Invoked Function Expression)

A

IIFE (Immediately Invoked Function Expression) is a JavaScript function that runs as soon as it is defined. The signature of it would be as below,

22
Q

How do you decode or encode a URL in JavaScript?

A

encodeURI() function is used to encode an URL. This function requires a URL string as a parameter and return that encoded string. decodeURI() function is used to deocde an URL. This function requires an encoded URL string as parameter and return that decoded string.

Note: If you want to encode characters such as / ? : @ & = + $ # then you need to use encodeURIComponent().

let uri = "employeeDetails?name=john&occupation=manager";
let encoded_uri = encodeURI(uri);
let decoded_uri = decodeURI(encoded_uri);
23
Q

What is memoization

A

“Memoization is an optimization technique where expensive function calls are cached such that the result can be immediately returned the next time the function is called with the same arguments”

24
Q

What is Hoisting

A

Hoisting is the default behavior of moving all the declarations at the top of the scope before code execution.

Note: JavaScript only hoists declarations, not the initializations.

25
Q

What are classes in ES6

A

Classes are a template for creating objects. They encapsulate data with code to work on that data. Classes in JS are built on prototypes but also have some syntax and semantics that are not shared with ES5 class-like semantics.