Skip to content

devnirajc/javascript-interview-questions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 

Repository files navigation

JavaScript Interview Questions & Answers

Click ⭐if you like the project. Pull Request are highly appreciated.

Table of Contents

No. Questions
1 What are the possible ways to create objects in JavaScript?
2 What is prototype chain?
3 What is JSON and its common operations
4 What is the purpose of array slice method?
5 What is the purpose of array splice method?
6 What is the difference between slice and splice?
7 How do you compare Object and Map?

What are the possible ways to create objects in JavaScript?

There are many ways to create objects in javascript as below,

  1. Object constructor:

The simpliest way to create an empty object is using Object constructor. Currently this approach is not recommended.

var object = new Object();
  1. Object's create method:

The create method of Object creates a new object by passing the prototype object as a parameter

var object = Object.create(null);
  1. Object literal syntax: The object literal syntax is equivalent to create method when it passes null as parameter
var object = {};
  1. Function constructor: Create any function and apply the new operator to create object instances,
function Person(name){ var object = {}; object.name=name; object.age=21; return object; } var object = new Person("Sudheer");
  1. Function constructor with prototype: This is similar to function constructor but it uses prototype for their properties and methods,
function Person(){} Person.prototype.name = "Sudheer"; var object = new Person();

This is equivalent to an instance created with an object create method with a function prototype and then call that function with an instance and parameters as arguments.

function func {}; new func(x, y, z); **(OR)** // Create a new instance using function prototype. var newInstance = Object.create(func.prototype) // Call the function var result = func.call(newInstance, x, y, z), // If the result is a non-null object then use it otherwise just use the new instance. console.log(result && typeof result === 'object' ? result : newInstance);
  1. ES6 Class syntax: ES6 introduces class feature to create the objects
class Person { constructor(name) { this.name = name; } } var object = new Person("Sudheer");
  1. Singleton pattern: A Singleton is an object which can only be instantiated one time. Repeated calls to its constructor return the same instance and this way one can ensure that they don't accidentally create multiple instances.
var object = new function(){ this.name = "Sudheer"; }

What is prototype chain?

Prototype chaining is used to build new types of objects based on existing ones. It is similar to inheritance in a class based language. The prototype on object instance is available through Object.getPrototypeOf(object) or proto property whereas prototype on constructors function is available through object.prototype.

What is the difference between Call, Apply and Bind?

Call: The call() method calls a function with a given this value and arguments provided individually Apply:

What is JSON and its common operations?

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

What is the purpose of array slice method?

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. Some of the examples of this method are,

let arrayIntegers = [1, 2, 3, 4, 5]; let arrayIntegers1 = arrayIntegers.slice(2,2); // returns [1,2] let arrayIntegers2 = arrayIntegers.slice(2,3); // returns [3] let arrayIntegers3 = arrayIntegers.slice(4); //returns [4]

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

What is the purpose of array splice method?

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 option second argument indicates the number of elements to be deleted. Each additional argument is added to the array. Some of the examples of this method are,

let arrayIntegersOriginal1 = [1, 2, 3, 4, 5]; let arrayIntegersOriginal2 = [1, 2, 3, 4, 5]; let arrayIntegersOriginal3 = [1, 2, 3, 4, 5]; let arrayIntegers1 = arrayIntegersOriginal1.splice(0,2); // returns [1, 2]; original array: [3, 4, 5] let arrayIntegers2 = arrayIntegersOriginal2.splice(3); // returns [4, 5]; original array: [1, 2, 3] let arrayIntegers3 = arrayIntegersOriginal3.splice(3, 1, "a", "b", "c"); //returns [4]; original array: [1, 2, 3, "a", "b", "c", 5]

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

What is the difference between slice and splice?

Some of the major difference in a tabular form

Slice Splice
Doesn't modify the original array(immutable) Modifies the original array(mutable)
Returns the subset of original array Returns the deleted elements as array
Used to pick the elements from array Used to insert or delete elements to/from array

How do you compare Object and Map?

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.

  1. The keys of an Object are Strings and Symbols, whereas they can be any value for a Map, including functions, objects, and any primitive.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. A Map may perform better in scenarios involving frequent addition and removal of key pairs.

About

List of 1000 JavaScript Interview Questions

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published