When I say tricky means a question-answer is never what you expect it to be. I just to mess with your mind and when you get such questions you need to read them carefully and understand them before answering the question. So here I have brought for you some of the trickey JavaScript questions that can help you to crack your interview.
Question 1
The first question is let’s say if I have two statements like given below. What would be the answer to each and why?
console.log(2 + '2');
console.log(2 - '2');
Answer
So the answer to this question is: For the first one the answer should be 22 and for the second one the answer should be 0.
console.log(2 + '2'); //22
console.log(2 - '2'); //0
Explanation
The reason is when I use + operator which means a + operator can be applied to either numbers or strings. When I have two numbers it would add two numbers. When I don’t have two numbers, what it would do is concat them. So we converted the second one into a string and concat them. That’s why I get 22 because of the concatenation.
In the second one, I’m getting 0 because the minus sign is not is only a number operator. This means it won’t work on any string. So what it would do is it would convert this string to a number and since it’s a number string it would easily convert it into 2. Consequently, 2 minus 2 is 0. So yeah we started with a simpler one. It’s a tricky question but it’s not that difficult.
Question 2
Alright, so I have this string of numbers [1 2 2 3]. Now I want you to remove the duplicates from this array. So basically once I remove them I should be getting something like this: [1 2 3] so that the second 2 is removed. Now in order to do that, I don’t want you to use any for loop or any kind of loop. I don’t want you to use any Map Reduce. For all those functions I want you to only use a single line to do this.
let nums = [1, 2, 2, 3]; // [1,2,3]
Answer
There may be multiple solutions to it. But I’ll give you one and if you know any other solution, feel free to do that as well.
console.log([...new Set(nums)]);
Explanation
So one way to do is is javascript has introduced a new data structure called set. You can do a new set and inside if I give a bunch of numbers then it would remove duplicates because set doesn’t allow any duplicate values. So if I just pass this array inside and console.log and run this I would get a set that has 1 2 and 3. However, I’m getting a set not actually an array. So in order to convert into an array, I can use a spread operator, and once I do that it would convert into an array.
Question 3
Our next question is: I have a function and I have a block. Within that, I have two variables L and V . Where L is defined using let and V is defined using the var keyword. As you know let has block scope and var has function scope. This means if I console.log(V) it would print V because it’s available throughout the function. But L doesn’t exist outside here it only exists inside this block.
let func = function() {
{
let l = 'let';
var v = 'var';
}
console.log(v);
console.log(l);
}
func();
So if I run this it would print var but it would say L is not defined. Now what I want you to do is I want you to fix this. I also don’t want var to be available outside.
Answer
So you want to use the var keyword to define this variable don’t change that. Usually, what happens when you use a transpiler so basically if you’re doing angular react you write your code in es6. Where you use let but that gets converted into var if your target is es 5 or so. That’s what happens you know your let gets converted to var but you need to do something to fix it. Because then var has function scope and not block scope. So you need to do something to make it a block scope. How can you fix this? So the answer is using an immediately invoked function expression. If I do that and wrap the entire thing into another function it’s quite ugly and then so my block is replaced by this
let func = function() {
{
(function() {
let l = 'let';
var v = 'var';
})();
}
console.log(v);
console.log(l);
}
func();
So now if I run this it would say V is not defined. Whatnot going further than this because L is also not defined but you would give me an error here. So this is how you do it. If you don’t know what immediately invoked function expression is I I’ll provide a link here.
Question 4
Alright so our next question is :
console.log(5 < 6 < 7);
console.log(7 > 6 > 5);
What’s the answer to both of them?
Answer
console.log(5 < 6 < 7); // true
console.log(7 > 6 > 5); //false
Explanation
console.log(true < 7);
console.log(true > 5);
In the answers, the first one should give true and the second one should give false. Even though they’re supposed to be true right because 7>6>5 and so they both supposed to be true. But one we are getting true and one is false. That is because the answer to 5<6 and is true in the first question. Also in the second one 7>6 is true. Now actually when you use a less than operator it will convert true into 1 in both the questions. Thus is 1<7? Yes. Is 1>5? no, it isn’t so you have false.
console.log(1 < 7); //true
console.log(1 > 5); //false
Question 5
Moving to our next question. I have an arrow function called a and it basically returns arguments. So what would be the answer to this:
let a = () => { return arguments};
console.log(a('hi'));
Answer
The answer to this is tricky. So if I have a regular function let’s say if I convert this into a regular function. Then because it’s a regular function I have to basically return arguments and so it would give me all the arguments. Hence I get a “hi”.
let a = function(){ return } arguments;
console.log(a('hi'));
But if I have an arrow function then I don’t get that “hi” back. That is because arguments do not bind to the arrow function very well. that’s why don’t use arguments inside an arrow function. You should rather use (…n) and return n. Now if I run this I would get an array with “hi”. So this is a better way to do it rather than using an argument.
let a = (...n) => { return n};
console.log(a('hi'));
Question 6
I’m sure a lot of you have seen this one but I’m just gonna put it anyway because it’s an interesting one. So let’s say if I have a function let x and inside I am going to do this return. It’s returning some object message if I execute x what would I get?
let x = function(){
return
{
message: 'hi'
}
}
x();
Answer
If I console.log it I would get undefined. That is because when I was writing it I realize that it’s actually giving you the error. So if I have a function return it should not have a line break. But here these two ( “return” and “{ message : 'hi' }” ) become separate statement. That’s why you are getting undefined. If I want to return something I would have to do this:
let x = function(){
return {
message: 'hi'
}
}
console.log(x());
After that, if I run this then I would get a “hi”.
Explanation
That is because in JavaScript you don’t need to enter a semicolon. So whenever I have something like this:
return
{
It automatically inserts a semicolon. So it does it like this:
return:
{
Which is bad actually so that’s why you’re getting that error or undefined result.
Question 7
I had this object profile with some property name. So what I want to do is if a user tries to add a new property and say profile.age = 3. I want users to prevent from doing this and so if I console.log after the profile it should only have one property. So how can I prevent users from adding new more properties?
let profile = {
name: 'techsith'
};
profile.age = 3;
console.log(profile);
Answer
So the answer to this question is:
let profile = {
name: 'techsith'
};
Object.freeze(profile);
profile.age = 3;
console.log(profile);
I would have to do something like Object.frreeze(profile). Object has a method called freeze and if I use this on the profile it would not allow me to add new properties. So if I’ve done Object.frreeze(profile); I would only get techsith I don’t see their age.
Hope so that you could have learned something. Share this article with your friends and press the notification icon to get updated with more such contents.