15 advance and basic JavaScrpt Interview Questions
By: Md Khalid Hossain
Tags: Javascript Questions, Map, Arrays, JavaScript, Javascript Interview

What are the possible ways to cretae objects in JavaScript?

Ans : There are many ways to create objects in JavaScript as below

i)Object constructor:

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

var Object = new Object()

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

iii)Object literal syntax:

The object literal syntax is equivalent to create method when it passes null as parameter

var object = {};

iv)Function constructor:

Create any function and apply the new operator to create object instances,

v)Function constructor with prototype:

This is similar to function constructor but it uses prototype for their properties and methods,

function Person(){}

Person.prototype.name = "shakil";

var object = new Person();

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

funtion func{};

new func(x , y , z)

2. What is the difference between Call, Apply, and Bind?

ans: The difference between Call, Apply, and Bind can be explained with the below examples,

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.

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

Call and apply are pretty interchangeable. Both execute the current function immediately. You need to decide whether it’s easier to send in an array or a comma-separated list of arguments. You can remember by treating Call as for comma (separated list) and Apply is for Array.

Whereas Bind creates a new function that will have this set to the first parameter passed to bind().

3. What is JSON and its common operations?

Ans: 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

4. What is the purpose of the array slice method

Ans: 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(0,2); // returns [1,2]

let arrayIntegers2 = arrayIntegers.slice(2,3); // returns [3]

let arrayIntegers3 = arrayIntegers.slice(4); //returns [5]

5. What is the purpose of the array splice method

Ans : 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.

Some of the examples of this method are,

6. What is the difference between slice and splice

Ans : Some of the major difference in a tabular form

Slice :

i)Doesn’t modify the original array(immutable)

ii)Returns the subset of original array

ii)Used to pick the elements from array

Splice:

i)Modifies the original array(mutable)

ii)Returns the deleted elements as array

iii)Used to insert or delete elements to/from array

7. How do you compare Object and Map

Ans : 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.

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

Ans : 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,

i)Two strings are strictly equal when they have the same sequence of characters, same length, and same characters in corresponding positions.

ii)Two numbers are strictly equal when they are numerically equal. i.e, Having the same number value. There are two special cases in this,

iii)NaN is not equal to anything, including NaN.

a)Positive and negative zeros are equal to one another.

b)Two Boolean operands are strictly equal if both are true or both are false.

iv) Two objects are strictly equal if they refer to the same Object.

v) Null and Undefined types are not equal with ===, but equal with ==. i.e, null===undefined → false but null==undefined → true

9. What are lambda or arrow functions

Ans: 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.

10. What is a first class function

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

const handler = () => console.log ('This is a click handler function');

document.addEventListener ('click', handler);

11. What is the difference between let and var

Ans: You can list out the differences in a tabular format

Var :

i) It is been available from the beginning of JavaScript

ii) It has function scope

iii) Variables will be hoisted

let :

i) Introduced as part of ES6

ii) It has block scope

iii) Hoisted but not initialized

Let’s take an example to see the difference,

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

Ans : 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.

13. What is IIFE(Immediately Invoked Function Expression)

Ans : 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,

(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. i.e, If you try to access variables with IIFE then it throws an error as below,

(function ()

{

var message = "IIFE";

console.log(message);

}

)

();

console.log(message); //Error: message is not defined

14 . What is Hoisting

Ans : Hoisting is a JavaScript mechanism where variables and function declarations 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,

15 . What is a cookie

Ans: 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,