10 JavaScript common interview questions.

T

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:

  • f

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)

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

7. Block Scope

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

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