----------------------------------------------
var a = 10;
let b = 20;
console.log(this.a) => 10
console.log(this.b) => undefined
variable defined using var has global scope so we can access it globally
variable defined using let has block scope so we can not get it
----------------------------------------------
(function(){
setTimeout(()=>console.log(1),2000)
console.log(2)
setTimeout(()=>console.log(3),0)
console.log(4)
})()
output:- 2 then 4 then 3 then 1
----------------------------------------------
setImmediate(()=>{
console.log("first")
})
Process.nextTick(()=>{
console.log("second")
})
console.log("Third")
=> output:- Third, second, first
----------------------------------------------
for(var i=0; i<10; i++){
setTimeout((function(i){
console.log(i)
})(i),10)
}
output:- 0,1,2,3,4,5,6,7,8,9
----------------------------------------------
for(var i=0; i<10; i++){
((x)=>{
setTimeout(function(){
console.log(x)
}x*1000)
})(i)
}
output:- 0,1,2,3,4,5,6,7,8,9
----------------------------------------------
(function(){
var x = 43
(function random()){
x++
console.log(x)
x = 21
})()
})()
output:- NAN => as in function x is undefined then add to undefined = NAN
----------------------------------------------
Promise.resolve(1)
.then((x)=>x+1) //2
.then((x)=>{throw new Error("My Error")})
.catch(()=>1) //1
.then((x)=>x+1) //2
.then((x)=>console.log(x)) //2
.catch(console.error)
output:- 2
----------------------------------------------
const a = {'key':'value'}
const obj1 = a
a.key = value2
console.log(a) => {key:value2}
console.log(obj1) =>{key:value2}
----------------------------------------------
console.log(false == '0') ==> true => 0 is falsy value
console.log(false === '0') ==> false => it will also check type which is bool and string
----------------------------------------------
var a = 10
console.log(a)
function Task(){
var a = 20
console.log(a)
if(true){
a = 30
console.log(a)
}
console.log(a)
}
task()
output:- 10, 20, 30, 30, 10
let a = 10
console.log(a)
function Task(){
let a = 20
console.log(a)
if(true){
a = 30
console.log(a)
}
console.log(a)
}
task()
output:- 10, 20, 30, 20, 10
----------------------------------------------
=> will below fun work ?
goodMor("riya")
console.log(goodMor) // undefined
var goodMor = function (name){
console.log(name)
}
=> no => goodMor is not function
---------------
goodMor("riya")
console.log(goodMor) // error
let goodMor = function (name){
console.log(name)
}
=> no => goodMor can not access before initializataion
----------------------------------------------
function printThis(){
console.log(this)
}
printThis() // global obj is getting printed
let obj ={a:10,b:20,c:printThis}
obj.c()
----------------------------------------------
get output without let
for(var i=1; i<=2; i++){
setTimeout(function(){alert(i)},100)
}
output:-
for(var i=1; i<=2; i++){
print(i)
}
function print(i){
setTimeout(function(){
console.log(i)
},100)
}
----------------------------------------------
get the second last element for the array, array length keep changing
let arr = ["a","b","c","e"]
output:- arr[arr.length -2]
----------------------------------------------
console.log(3+'3') => 33
console.log(3-'3') => 0