JavaScript Interview Questions that made me think — Do I really know JavaScript? — Part 3
Javascript tricky interview questions
Javascript is full of surprises, Here I am sharing few question which seems very interesting.
What will be printed in the console?
.
.
.
Error will be thrwon in the console:
Uncaught SyntaxError: identifier starts immediately after numeric literalThis is due to language grammar limitation.
The dot(.) character could mean different things in JS. It can be seen as the member operator, or a decimal, depending on its placement.
In order to make above code work we have to either use parenthesis or an additional dot to make the expression valid.
21..toString(); // "21";
(21).toString(); // "21";What will be printed in the console?
.
.
.
.
4520, 92So First operation is obivious, whenever we do add(+) operation with string,
JS will concatanate the string.
But for ++ what went different?
It's the implementation of ++ and ++, both of these first convert the argument
to Number and then do a ++.
function plusplus(number) {
const num = Number(number);
num = num+1;
return num';
}What will be printed in the console?
.
.
.
.
true
Let's break it down.
Whenever we do a double equality check, JS engine will try to convert both
values in Number.
Number([]) == Number(![]);
0 == Number(false); // ![] will give false
0 == 0 // trueWhat will be returned from the function?
.
.
.
.
90000
Here if we break down this as,
const res = cosmic = 90000;
return res;
// Undeclared cosmic will be added to global scrope and 90000 will be returned.What will be logged in the console?
.
.
.
.
Water Added
Uncaught TypeError: addSugar is not a functionWhat will be printed in the console?
.
.
.
.
3, Hello
this is pointing to array itself, and the length of array is 3.What will be logged in the console?
.
.
.
.
[ 10, 20, 3, 7 ]By default, sort method performs sorting by converting the numbers into strings. Then it compares their sequences of UTF-16 code unit values.
To sort the numbers by their numeric value, we’ll need to pass a compareFn to the sort() method:
[ 10, 20, 3, 7 ].sort((a, b) => a - b); //[ 3, 7, 10, 20]In case you have any doubt or want to understand any concept related to web development, book as session with me on topmate. https://topmate.io/frontendmaster
Here are the other parts of this series:
