Some javascript interview question that you must know

Nahid Hossain Nehal
4 min readNov 5, 2020

1. Difference between slice() and splice() methods:

slice():

The slice() method can take only two arguments. The first parameter of the slice() method is the starting index and the next one is the ending index. It’s doesn’t change the main array it returns a new array.

Syntax: array.slice(startIndex, endIndex)

splice():

On the other hand the splice() method can take several parameters. The first one is the starting point and the next one is the number of items you want to remove and after that, you can pass as a parameter those items you want to add new. It will return you deleted items and you will find your main array as modified.

Syntax: array.splice(startIndex, totalItem, item 1, item 2, …..item n)

2. What is closure()

If I return or use any function from another function then it’ll create a closed environment and if the inner function which one we will return, that function use any outer variable then it’ll save a reference value of an external variable and this is called clouser(). The clouser() is one of the core things of javascript. We can build a tiny React hook by closure()

3. What is Hoisting

If I declared any variable anywhere into scope as a var then it will move the declaration part to the top of the scope it is called hoisting. It will move the only declaration part but the initialization part will be at the same place and it’s a general attitude of javascript. If I use any variable before initialization then it will give undefine.

4. (== vs ===) Difference between double equal and Triple equal

It is one of the most common questions in javascript interviews. You also might be asked this question. Ok let’s go ahead with this one

Triple equal (===) means it will check the value and also data type if both are matched then it will return true if anyone doesn’t match it will return false.

On the other hand double equal (==) means it will check only value, not the data type. It will firstly make data type correction based upon the values of variables and then check the variable's value.

5. Null vs Undefined

In javascript, you can get undefined in many ways.

  1. If you want to access any objects property that doesn’t exist there then it will return undefined.
  2. If you want to access any variable without initializing then you will get undefined.
  3. If you don't pass the function's parameter which you used that time you will get undefined
  4. If you want to access any value from an array that does not exist.
  5. And you can also set a value as undefined but which is not recommended.

And many other ways you can get undefined.

But you don’t get any null value by default. You have to set the value explicitly as a null when you needed.

6. Truthy vs Falsy

In programming, there are two types of boolean value one is true and another is false.

The truthy values are given below:

  1. ”0"
  2. " "
  3. {}
  4. []
  5. ture
  6. "false"

The falsy values are :

  1. undefined
  2. null
  3. NaN
  4. ""
  5. 0
  6. false

7. Callback Function

If you pass any function as a parameter of another function it’s called callback function. An example is given below:

8. Factorial in Recursive way:

9. Fibonacci in Recursive way:

10. Fibonacci Series By Recursive Way

--

--