10 JavaScript interview questions that you need to know.
Today I’m gonna discuss top 10 interview question which you should know as a JavaScript developer. And if you have a clear concept about this topic then you also can answer easily in the interview.

1. Truthy and Falsy Value
There are some criteria for truthy and falsy value. Truthy value is shown true output and falsy value is shown false output. Let’s see the criteria for truthy and falsy.
//truthy value for a number
// '0', ' ', [], {} is a truthy value
const value = 4;
if(value){
console.log('true')
}
else{
console.log('false')
}
// output: true
//falsy value for a number
const value2 = 0;
if(value2){
console.log('true')
}
else{
console.log('false')
}
//output: false
// 0, '', undefined, null, NaN, false is a falsy value
2. Undefined vs NULL
There is a massive difference between undefined and null. When a developer declares a variable but not assign a value or doesn’t return anything when using a function or using the function but doesn’t pass the parameter then it shows undefined. That means undefined means something is a mistake by the developer. And null means it's an empty value assigned by the developer. For many purposes, the developer assigns a null value.
3. Double equal vs triple equal
‘==’ and ‘===’ use to check two values are the same or not but there is also the difference between these two. Double equal (==) is check just two values that are the same or not. It does not check the type of value. Suppose if I compare between 2 and ‘2’ then it shows ‘true’, but here one is a string and another is a number. But triple equal check as well as two values are same or not and their type also same or not. If I compare 2 and ‘2’ then it shows false. Because one is a string and another is a number.
4. Scope, block scope
When you declare a variable inside a block using let or const then you can’t access it from outside. But if you declare it using var then you can access it from anywhere. Because we know that using var before a variable, it hoisted on the top.
//example using let or const
function add(num1, num2){
let result = num1 + num2;
return result
}
const res = add(10,20)
console.log(res)
console.log(result) // can't access result outside of a function//example using var
function add2(num1, num2){
var result2 = num1 + num2;
return result2
}
const res2 = add2(10,20)
console.log(res2)
console.log(result2) // now i can access result2 from here
5. Closure
When a function is declared inside another function and if this second function is used or returns anything then it makes a closed environment and makes an external variable as a reference.
function Watch(){
let count = 0;
return function(){
count++;
return count;
}
}
const clock1 = Watch()
console.log(clock1()) //1
console.log(clock1()) //2
console.log(clock1()) //3const clock2 = Watch()
console.log(clock2()) //1
console.log(clock2()) //2
console.log(clock1()) //4
6. ‘This’ Keyword
This is a special keyword in JavaScript. It refers to which object it belongs to. In a method, it refers to the owner object and in a function, it refers to the global object.
const Student = {
firstName: 'Iqbal',
lastName: 'Ahmed',
fullName: function(){
return this.firstName + ' ' + this.lastName;
}
};
In this example, this represents the Student object.
7. Window, Global Variable
All JavaScript code is executed in an environment and most probably the environment will be a browser. Our code is executed in a root file and in a browser, the root file is a window.
var name = 'iqbal ahmed'
//now you can access this using window in your browser
conosole.log(window.name)
//output: iqbal ahmed
we often use the console, document, etc. All of these things have a global variable and that is the window.
Now global variable is a variable that I can access from anywhere.
var name = 'iqbal ahmed';
function (){
var id = 173462142
console.log(id) /its a local variable, only can access here
console.log(name) //name is global i can access anywhere
}
console.log(id)// i can access it from here
8. Find the largest element of an array
Consider the following program
var num = [30,0,20,40,50,100,90,60]
var max = num[0]
for(let i=0; i<num.lenght; i++){
let element = num[i];
if(element > max){
max = element;
}
}
console.log(max); // output:100
9. Remove Duplicate eLement from an array
An array can contain the duplicate values. Our task is to remove all duplicate values from an array. So Consider the following program
var num = [10,20,10,43,20,54,76,43]
var uniqueNum = []
for(var i=0; i < num.length; i++){
var element = num[i];
var index = uniqueNum.indexOf(element);
if(index == -1){
uniqueNum.push(element)
}
}
console.log(uniqueNum) //output: 10, 20, 43, 54, 76
10. Count the number of words in a string
var mySpeach = 'Hi everyOne! This is a JavaScript Tutorial';
var count = 0;
for(var i=0; i< mySpeach.length; i++) {
var char = mySpeach[i];
if(char == ' ' && mySpeach[i-1] !== ' '){
count++;
}
}
count++;
console.log(count) //output: 7
Here every word separates using ‘ ’ space. That’s why just count the space number and remove duplicate space.