Truthy or Falsy Value
In Javascript, Truthy means true while it is using as a boolean purpose. And Falsy means false as a boolean purpose. Except few values all values are truthy. The falsy values are 0, "", null, false, NaN, Undefined Few confusion show in below:
var a = 0;
if(a){
console.log("This is truthy");
}else{
console.log("This is falsy");
}
The above code executes This is falsy because a=0 is falsy value.
var a = "";
if(a){
console.log("This is truthy");
}else{
console.log("This is falsy");
}
The above code also executes This is falsy because a="" is falsy value.
var a = " ";
if(a){
console.log("This is truthy");
}else{
console.log("This is falsy");
}
The above code also executes This is truthy notice in a declaration and assigned a string White space. " " this is a truthy value.
Null Vs Undefined
Undefined is a datatype. It means a variable declares but the value is not assigned and Null is a value that can assign in a variable. The example is shown below
var hello; //variable declare but value not assigned
console.log(hello); // print here undefined
another way to get undefined
var hello = "hi"; //value assigned hi
console.log(hello);
hello = undefined; //undifined assign to hello
console.log(hello);
hello = null;
console.log(hello); // print here null
Double equal (==) Vs Triple equal (===)
Double equal and triple equal use to compare two values.
Difference here
Triple equal (===) not only compare value but also variable datatype also. While both are the same then it is true.
var a = 77;
var b = '77';
console.log(a===b); // its false, values are same but datatype
var a =77;
var b=77;
console.log(a===b); //its true
var a = 'hello';
var b = 'hello';
console.log(a===b) //its true,
Double Equal only compare value don't datatype.
var a = 77;
var b = '77';
console.log(a==b); // its true, values are same
Scope
Scope means a variable accessible range.
Global Scope
If a variable declares in global scope it can access anywhere. The variable is accessible to function scope and block scope.
var name= 'khan'
console.log(name); //khan
function getName(){
console.log(name); //name is accessible here
}
Block Scope
It means a variable is accessible to a block likes if, switch, for, while, etc. Block means an area of code that start and end Carly brace {blcok scope area}
function getName(){
let name1= 'khan'; //exist in function and block
if(true){
let name2= 'momin'; //exist in block scope
}
console.log(name1); //it is in the block
console.log(name2); //it is out of its block
}
Clouser
Closure definition is given by Douglas Crockford :
Closure means that an inner function always has access to the vars and parameters of its outer function, even after the outer function has returned.
function outerFunction(){
var a = 5;
function innerFunction(){
console.log(a);
}
return innerFunction; //here return the reference of the innerFunction
}
var innerF = outerFunction(); //innerF assigned reference of innerFunction.
innerF(); //call here innerFunction and print 5
The above code outerFunction() return the reference of innerFuncion(). Though innerFunction() is in the outerFunction() block scope. But it can access out of the outerFunction() block. This is clouser.
Top comments (0)