Ten JavaScript Interview Questions You have to Know

Md Naimur Rahman Durjoy
4 min readMay 8, 2021

--

1.Truthy or Falsy

javaScript determines some values which can true or false.

How to memories it?

· There are all the variables that has a value except 0(zero) is true· There are all strings are true except “empty string”.

Falsy:

False, 0, “”,undefined,null, NAN

Truthy:

‘0’, ” ”, [],{},’false’

How to check it?

const variableName = undefined;if(variableName){console.log(“this is true”);}else{console.log(“this is false”);}Output:this is false

Note: “0” (zero) and ‘ ’(white space) are true

2.Null vs Undefined

Null: Null means empty value that assigns before but that value gets away from the variable.

Undefined:

We can get undefined some ways that we discuss below:

· If we not assign a value in variable· Forget to return something from function· Don’t assign all the parameters of the function· Don’t assign a value in the object but read that· Don’t assign a value in the array but read that· If the variable consist a value of undefined

3.(==) vs (===)

This is a simple but important topic, What the difference between double and triple equal

Double equal: it just read the value of the property

const variableName = 14;if (variableName == “14”) {console.log(“this is true”);}else {console.log(“this is false”);}Output:this is true

Triple equal: it read the value of the property but also read the type of that property

const variableName = 14;if (variableName === “14”) {console.log(“this is true”);}else {console.log(“this is false”);}Output:this is false

4.what is hoisting?

A variable can be used before it declares this is called hoisting.

console.log(name);var name = “naim”output:undefinedvar name = “naim”console.log(name);output:naimlet name = “naim”console.log(name);output:naimconsole.log(name);let name = “naim” //let or constoutput:error

at first, we declare a variable using var and output of it undefined, second and thirdly we get a result but at the last, we get an error, var make a global scope, where let or const make a block scope, for this reason, this happened.

5.what is closure?

If we call or return a function inside a function , these functions create a close environment between them this situation is called as closure or encapsulation or private variable,

const result1 = counter();const result2 = counter()console.log(result1());console.log(result1());console.log(result2());console.log(result2());console.log(result2());output:
1
2
1
2
3

6. var vs let vs const

We know in javascript a variable can declare by var or let or const. In 2015 when ES6 arrive we first knew about let and const . var create a global scope but let and const create a block scope. Var or let can be updated and reassign its value but in const we can not reassign values.

7.how to calculate factorial of a number using for loop?

Firstly we declare a variable named number and initial value of the num of factorial equal to zero,

num! =(num)*(num-1)*(num-2)…….*1

the we apply a for loop from 1 to index of the num ,and multiply the factorial by i ,

and finally we got our answer

const num = 5;let factorial = 1;for (let i = 1; i <= num; i++) {factorial = factorial * i;}console.log(factorial);output:120

8.is the number prime or not?

Definition of a prime number is exactly not divisible by all number except 1 and the same number.

19 is a prime number because it is only exactly divisible by 1 and 19.

Code of the program:

function isPrime(number) {for (let i = 2; i < number-1; i++) {if(number%i===0){return “its not a prime number”;}else{return “its a prime number”;}}}const result = isPrime(19)console.log(result);output:its not a prime number

9.how to find largest value of an array?

Let the maximum value is the first element of the array that index is 0(zero).

Now apply a for loop from first to last index of the array, and compare the element value with max value

Code of the program:

const array = [10, 20, 30, 40];let max = array[0];for (let i = 0; i < array.length; i++) {const element = array[i];if(element>max){max=element;}}console.log(max);output:40

10.how to remove duplicate item from an array?

Let a uniqueNumber equal to [] (empty array), then apply a for loop from first to end of the index of the array, and check the index number of unqueNumber, if index number is -1 then push the value into unqueNumer.

Code of the program:

const array = [10, 20,30,14,15,20,40 ,30, 40];let uniqueNumber = [];for (let i = 0; i < array.length; i++) {const element = array[i];const index = uniqueNumber.indexOf(element)if(index==-1){uniqueNumber.push(element)}}console.log(uniqueNumber);output:[ 10, 20, 30, 14, 15, 40 ]

--

--

Md Naimur Rahman Durjoy