Some Interview Questions of JAVASCRIPT
01. Truthy and Falsy value in JavaScript
As JavaScript is a programming language, it has 2 functional values. One is are ‘true’ and the other is ‘false’. These values are called Boolean values.
Some Truthy values: negative number to positive number (except 0), empty Array, empty Object, string with values (“ ” if u give a blank value, it will also return true), “0”(this also return true. As it acts like a string not like a number), true,
Let’s see some code example:
01. const data = " "if(data){
console.log('TRUE') // true
}
else{
console.log('FALSE')
}02. const data = "0"if(data){
console.log('TRUE') // true
}
else{
console.log('FALSE')
}03. const data = {}if(data){
console.log('TRUE') // true
}
else{
console.log('FALSE')
}04. const data = -10if(data){
console.log('TRUE') // true
}
else{
console.log('FALSE')
}//you can test all the examples by yourself and every time it will return true value
Some Falsy values: 0, null, NaN, undefined, string without values (“”), false,
Let’s see some code example:
01. const data = falseif(data){
console.log('TRUE')
}
else{
console.log('FALSE') // FALSE
}02. const data = ""if(data){
console.log('TRUE')
}
else{
console.log('FALSE') // FALSE
}03. const data = null // same with NaN and undefinedif(data){
console.log('TRUE')
}
else{
console.log('FALSE') // FALSE
}04. const data = 0if(data){
console.log('TRUE')
}
else{
console.log('FALSE') // FALSE
}//you can test all the examples by yourself and every time it will return false value
As a beginner, it takes some time to get this.
02. What is the difference between == and === operators?
JavaScript provides both strict (===
, !==
) and type-converting(==
, !=
) equality comparison. The strict operators take the type of variable into consideration, while non-strict operators make type correction/conversion based upon values of variables. The strict operators follow the below conditions for different types,
1. Two strings are strictly equal when they have the same sequence of characters, same length, and same characters in corresponding positions.
2. Two numbers are strictly equal when they are numerically equal. i.e, Having the same number value.
There are two special cases in this,
1. NaN is not equal to anything, including NaN.
2. Positive and negative zeros are equal to one another.
3. Two Boolean operands are strictly equal if both are true or both are false.
4. Two objects are strictly equal if they refer to the same Object.
5. Null and Undefined types are not equal with ===, but equal with ==. i.e,
null===undefined --> false
but null==undefined --> true
Some of the example which covers the above cases:
0 == false // true
0 === false // false
1 == "1" // true
1 === "1" // false
null == undefined // true
null === undefined // false
'0' == false // true
'0' === false // false
[]==[] or []===[] //false, refer different objects in memory
{}=={} or {}==={} //false, refer different objects in memory
03. What are lambda or arrow functions?
An arrow function is a shorter syntax for a function expression and does not have its own this, arguments, super, or new. target. These functions are best suited for non-method functions, and they cannot be used as constructors.
04 . What are closures?
A closure is the combination of a function and the lexical environment within which that function was declared. i.e, It is an inner function that has access to the outer or enclosing function’s variables. The closure has three scope chains
1. Own scope where variables defined between its curly brackets
2. Outer function’s variables
3. Global variables
Let's take an example of the closure concept:
function Welcome(name){
var greetingInfo = function(message){
console.log(message+' '+name);
}
return greetingInfo;
}
var myFunction = Welcome('John');
myFunction('Welcome '); //Output: Welcome John
myFunction('Hello Mr.'); //output: Hello Mr.John
05. What is scope in javascript?
The scope is the accessibility of variables, functions, and objects in some particular part of your code during runtime. In other words, scope determines the visibility of variables and other resources in areas of your code.
06. What is event bubbling?
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 in the same nesting hierarchy till it reaches the outermost DOM element.
07. What is event capturing?
Event capturing is a type of event propagation where the event is first captured by the outermost element, and then successively triggers on the descendants (children) of the target element in the same nesting hierarchy till it reaches the innermost DOM element.
08. Why is JavaScript treated as Single-threaded?
JavaScript is a single-threaded language. Because the language specification does not allow the programmer to write code so that the interpreter can run parts of it in parallel in multiple threads or processes. Whereas languages like java, go, C++ can make multi-threaded and multi-process programs.