Most Important JS interview questions
- What is falsy value?
Ans: A falsy value is a value that is considered false when encountered in a Boolean context. Here the falsy values:-
1. false
2. 0 (must without quotation "0" = truthy)
3. '' (empty string without white space i.e " " = truthy)
4. NaN
5. null
6. undefined
2. What is truthy value?
Ans: In JavaScript, a truthy value is a value that is considered true when encountered in a Boolean context. All values are truthy unless they are defined as falsy.
i.e. let name = false; // falsy value
let name = 'False'; // truthy value
3. null vs undefined?
Ans: In JavaScript, undefined is a type, whereas null an object.
undefined: Variable has been declared but no value is assigned/defined. A function is declared but has no return. If I don’t pass an argument to a function. And If I want to access undeclared value from an Object or Array.
let name; //undefined
null: null is an empty or non-existent value. null must be assigned.
let a = null; //null
4. Difference double equal (==) and triple equal (===)
double equal: double equal is an abstract equality comparison. It is used for the comparison of two variables but it ignores datatype of the variables.
triple equal: triple equal is a strict equality comparison. It compares both the value and datatype of variables. f the value is the same but the type is not, the equality will evaluate to false.
const num1 = 7;
const num2 = '7'console.log(num1 == num2); //true
console.log(num1 === num2): //fasle
5. Scope| Global scope vs Local scope
Scope refers to the current context of code. It determines from where a variable/function is accessible. Every function creates a scope. There are mainly two scopes in JS. 1. Local Scope and 2. Global Scope.
Global Scope: Variables defined outside any function, block, or module scope have global scope. Variables in the global scope can be accessed from everywhere on the application.
Local Scope: Variables declared inside any function are called local variables. Local variables cannot be accessed or modified outside the function declaration.
6. Closure
A closure gives you access to an outer function’s scope from an inner function. A closure is a function that has access to the variable from another function’s scope. It is the by-default behavior of JavaScript. Of course, the outer function does not have access to the inner scope