10 JavaScript common interview questions.

Mohammad Fahad
4 min readNov 5, 2020

The moment you become a developer, the subsequent question that involves your mind is how am I able to get employment? to urge a job you need to be a master in handling the interview questions.

During this blog, I’ve got tried to share a number of my experiences.

1. Truthy and Falsy values:
As well as a sort, each value also has an inherent boolean value, generally called either truthy or falsy. a number of the principles are a touch bizarre so understanding the concepts and effect on comparison helps when debugging JavaScript applications.
The following values are always falsy:

  • false
  • 0
  • ‘’ or “” (empty string)
  • null
  • undefined
  • Nan

The rest is truth. Which also includes:

  • ‘0’ (zero within a string)
  • ‘false’
  • [] (an empty array)
  • {} (an empty object)
  • And an empty function.

2. Null Vs Undefined

In JavaScript, undefined is a type, whereas null an object.

undefined

It means a variable declared, but no value has been assigned a value.

For example,

let demo;
console.log(demo); //shows undefined
alert(typeof demo); //shows undefined

null

Whereas, null in JavaScript is an assignment value. You can assign it to a variable.

For example,

let demo = null;
console.log(demo); //shows null
alert(typeof demo); //shows object

3. == Vs === (Double equals Vs Triple equals sign)

In any programming language, we use a single equals sign to assign a value.

Double equals compares/checks the value only.

And tripel equals compares value and type as well.

For example,

11 == '11' // returns true 
but,
11 === '11' // returns false

In the above example, we can clearly see that when we are checking the same thing with double and triple equals signs it shows different results.

4. Implicit coercion

This is a Javascript feature that is suggested to avoid, whenever you give an unexpected value js corrects it, runs the operation, and returns the desired result.

For example,

2 + '2' // returns 4
2 * [2] // returns 4

In the example, we can see that even we are adding a string ‘2’ with a number 2 it is returning the right answer.

5. Apply map, filter, find on an array of objects

When you have an array of objects and you want to loop through all the items use the map, when you want to find a specific item use find, and when you want a specific category of items use the filter.

There are many examples available you can go through one.

6. Scope

In JavaScript there are two forms of scope:

  • Local scope
  • Global scope

Each function has its own scope too.

Scope determines the visibility of variables, for instance, if you declare a variable inside a function scope it’ll not be accessed outside of the function scope.

7. Block Scope

A block scope is that the area within if, switch conditions or for and while loops. Generally speaking, whenever you see , it’s a block. In ES6, const and let keywords allow developers to declare variables within the block scope, which suggests those variables exist only within the corresponding block.

8. Find the largest element of an array

You can find the largest number of an array using 3 methods:

  • using a for loop
  • using the reduce() method
  • using Math.max()

I’m going to cover the easiest one, using the Math.max.apply() method.

const myArr = [2, 3, 7, 6, 4, 9];
console.log(Math.max.apply(null, myArr)); // returns 9

Note that the first argument to apply() sets the value of ‘this’, not used in this method, so you pass null.

for more details click here.

9. Sum of all numbers in an array

You can use the reduce() method to seek out or calculate the sum of an array of numbers.
The reduce() method executes the desired reducer function on each member of the array leading to one output value, as demonstrated within the following example:

const arr = [1, 2, 3, 4, 5];  
// Getting sum of numbers
const reducer = (accumulator, currentValue) => accumulator + currentValue;
console.log(array1.reduce(reducer)); // returns 15

10. Remove duplicate item from an array

You can use the indexOf() method in conjugation with the push() remove the duplicate values from an array or get all unique values from an array in JavaScript.

Let’s take a glance at the subsequent example to grasp how it basically works:

function findUnique(array){
let uniqueArray = [];

// Loop through array values
for(i=0; i < array.length; i++){
if(uniqueArray.indexOf(array[i]) === -1) {
uniqueArray.push(array[i]);
}
}
return uniqueArray;
}

const names = ["John", "Peter", "Clark", "Harry", "John", "Alice"];
let uniqueNames = getUnique(names);
console.log(uniqueNames); // Prints: ["John", "Peter", "Clark", "Harry", "Alice"]

That’s all for today :)

--

--

Mohammad Fahad

Innovative and Detail-Oriented Frontend Developer trying to write some articles as a hobby!