JavaScript Q&A Part-1
We’re going to talk about some cool questions and what happens when we answer them.
Question 1:
var num = 100;
if(num === '100')
{
console.log(true);
}
else
{
console.log(false);
}
Answer: false
The strict equality ( === ) operator checks if two things are the same, and gives a yes or no answer. It’s different from the regular equality operator because it doesn’t treat things as the same if they’re different types.
Question 2:
var person = {
name: 'Robert',
age: 34
}
console.log(person.address || null || undefined || 'Not Found');
Answer: Not Found
This is an example of short circuit evaluation. In JavaScript, the || operator gives you the first value that’s considered true among all the things you give it. If none of them are true, it just gives you the last value you gave.
Question 3:
var arrTest = [100,101,102];
arrTest[100] = 1000;
console.log(arrTest.length);
Answer: 101
The array index starts with 0. Here we are setting 101 elements of the array, so the length will be 101.
Question 4:
let name = "Ravi";
let isNameExist = fullName.indexOf("ravi") > -1;
console.log(isNameExist);
Answer: false
In JavaScript, all string functions are case-sensitive, meaning they treat uppercase and lowercase letters as different.
Thanks for reading.