JavaScript 10 basic interview questions and answer you should know
1)What are Truthy and Falsy values in JavaScript?
Truthy are expressions which evaluates to boolean true
value and falsy evaluates to boolean false
value. Falsy values are 0, “ ” , null , false ,Nan undefined etc . Any expression or value other than above listed falsy values is considered truthy .
let name = null
if(name){
console.log('Condition is true')
}
else{
console.log('Condition is false') //Output: condition is false
}
2) What is the difference between null and undefined in JavaScript?
In JavaScript, undefined is a type, whereas null an object. In undefined variable declared but no value assigned in it. Interpreter returns undefined accessing a variable or object property that is not yet initialized. On the other hand a null value has a defined reference to “nothing”
let student ={name:'nibir'}
console.log(student.id) // It will return undefinedlet student ={name:null}
console.log(student.name) //It will return null
3)What are the differences between double equals (==) and triple equals(===) in JavaScript ?
Double equals (==) in JavaScript is used for comparing two variables, but it ignores the datatype of variable. It returns True when two operands are equal. Triple equals (===) is used for comparing two variables, but this operator also checks datatype and compares two values. It returns true when both value and data types are same for the two variable.
0 == '0' // true
0 === '0' // false
4)what is global variable, global scope in JavaScript ?
A JavaScript global variable is declared outside the function or declared with window object. It can be accessed from any function. When you declare variable globally or outside the function it added window object internally .A global variable has global scope all function or scripts can access it.
var name = 'nibir';
function myName(){
console.log('inside function ', name)
}
console.log('Outside function ', name)
5)What is this keyword in JavaScript?
The JavaScript this keyword refers to the object it belongs to. It is actually refers to the current object in a method or constructor . In an object method, this
refers to the "owner" of the method.
var student = {
firstName: "Toasin",
lastName : "Nibir",
id : 5566,
fullName : function() {
return this.firstName + " " + this.lastName;
}
};
console.log(student.fullName()) //Output will be 'Toasin Nibir'
6)What is event bubble ?
Event bubbling is a type of event propagation where the event first triggers on the innermost target element, and then successively triggers on the ancestors (parents) of the target element. The event bubbles up from parent to parent until it is handled, or until it reaches the document object.
7)Remove duplicate item from an array
We can declare an empty array . Then we check if the index of each elements of the given array is in uniqueArray or not . If not then we push the element into uniqueArray.
8) Reverse a String:
In this problem solution we declared an empty string name newString. Then we start a for loop which count the given string from index 0 and we add it to our declared empty string and return it. Then it return the given string reverse order.
function reverseString(str) {
var newString = "";
for (var i = 0 ; i < str.length; i++) {
newString = str[i] + newString;
}
return newString;
}
reverseString('hello');
9) What is bind, call and apply in JavaScript ?
The call() method invokes a function with a given ‘this’ value and arguments provided one by one. Apply( ): Invokes the function and allows you to pass in arguments as an array. Bind(): returns a new function, allowing you to pass in an array and any number of arguments.
10) What is closure in JavaScript ?
Closures is used to enable data privacy. When you use closures for data privacy, the enclosed variables are only in scope within the containing (outer) function. You can’t get at the data from an outside scope except through the object’s privileged methods. A closure is a function having access to the parent scope .