Top 10 JavaScript Tricky Interview Questions You Must Know
Today I will discuss about 10 tricky question of JavaScript. JavaScript has been the most used programming language for so many years now, yet people find it hard to get a grasp on.
Let’s play —
There are some truthy and falsy values in JavaScript.
1. Falsy Values —
In JavaScript six things are falsy and they are-
i) false
ii) null
iii) undefined
iv)‘’
v) 0
vi) NaN
2. Truthy values —
There are only two truthy things true and everything that is not false.
Here is chart and programming testing of truthy and falsy values:
Now, I will discuss about null vs undefined. Sometimes a junior JavaScript has confused about null and undefined. Let’s talk about what is the difference between undefined vs null. undefined is a type, whereas null an object.
3. undefined—
The actual meaning of undefined is to say that a variable has declared but it has no value assigned first. Or simply a variable has been declared, but not defined. Finally, when looking up non-existent properties in an object, you will receive undefined.
Example: undefined
var d = {};console.log(d.fake);
// undefined
4. null —
A developer can assign it to a variable. Here, null in JavaScript is an assignment value.
5. Tripple Equal ‘===’ meaning
If we are using triple equals ===
in JavaScript, we are just testing for strict equality. This means that the both value and the type we are comparing have to be the same.
Example:
5 === 5
// true
'hello world' === 'hello world'
// true (Both Strings, equal values)true === true
// true (Both Booleans, equal values)
Other Example:
50 === '50'
// false (Number vs String)
This is false because we are comparing the number 50 to the string value of 50. Same value but different type.
6. Double Equal ‘==’
If we are using double equals ==
in JavaScript, we are just testing type coercion. Type coercion means that two values are compared only after attempting to convert them into a common type.
Simply, double equal are just testing the value.
From Previous Example:
50 == '50'
// true(Number vs String)
Here it shows that it’s true.
7. What is NaN?
Basically NaN means Not a Number. NaN is not equivalent to anything.
Example:
NaN == null
// falseNaN == undefined
// falseNaN == NaN
// false
8. What is Hoisting?
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 initialization.
Let’s take a simple example of variable hoisting:
console.log(abid); //output : undefined
var abid= 'The variable has been hoisted';
map, filter, find, smart way to run for loop —
9. map —
Basically, map returns an array with the same length. Map works on an array and return an array that’s it. Inside map there is three parameter we can pass first one is the element, second is index and third is array.
Example:
Let’s have a look of for loop: Here multiply the numbers.
let numbers = [4, 5, 6, 7, 8];
const output = [];for (let i = 0; i < numbers.length; i++) {
const element = numbers[i];
const result = element * element;
output.push(result);
}console.log("The output is: ", output);//The output is: [ 16, 25, 36, 49, 64 ]
Now for map system:
let numbers = [4, 5, 6, 7, 8];const output = numbers.map(function(element){
const multiply = element * element;
return multiply;
})console.log ("The output is: ", output);//The output is: [ 16, 25, 36, 49, 64 ]
Now the compact version of map with arrow function:
let numbers = [4, 5, 6, 7, 8];
const output = numbers.map(x => x * x);console.log ("The output is: ", output);//The output is: [ 16, 25, 36, 49, 64 ]
10. filter —
Filter works on array return an array for filtered items. Basically it’s return an array when it’s fulfilled the condition on a function.
Example:
let numbers = [4, 5, 6, 7, 8];
const output = numbers.filter(x => x > 5);console.log ("The output is: ", output);//The output is: [ 6, 7, 8 ]
The code shows the the bigger number, which is bigger than 5.
11. find —
Find works on an array and return the first element that satisfy the condition in function. It’s return the elements from an array.
Example:
let numbers = [4, 5, 6, 7, 8];
const output = numbers.find(x => x > 5);console.log ("The output is: ", output);//The output is: 6
That’s all for today. I will comeback with another topic.
Thanks.