#002 Javascript Interview Questions | JS Output based questions || JS Interview Prep #javascript

preview_player
Показать описание
#javascript #websuccess #websecurity #html #css #frontend #frontenddeveloper #frontendmaster #rahuulmiishra

Рекомендации по теме
Комментарии
Автор

if we pass the setTimeOut() into an function then pass i value by argument then using var it also solve this problem because now this time i use as reference .... like
for(var i = 1; i < 4; i++){
function show(i){
setTimeout(()=>console.log(i), 0);
}
show(i);
}

HassanMurtaza
Автор

for(var i=0;i<4;i++){
setTimeout((e)=>console.log(e), 5, i);
}

ProgrammingBoy
Автор

thats why var is considered faster than let and const

zalaam_
Автор

we can use IIFE here for 'var' cases

AnkitSharma-oqwi
Автор

//1st solution...closure
for(var a = 0; a < 5; a++){
function innerFunc(_a){
setTimeout(() => {
console.log(_a) ///this is an error how to resolve
}, 1000 * _a);
}
innerFunc(a)
}

//2nd solution...IIFE
for(var b = 0; b <= 5; b++){
(function(a){
setTimeout(() => {
console.log(a)
}, a * 1000)
})(b)
}

//3rd solution...let block scope
for(let i = 0; i <= 5;i++){
setTimeout(() => {
console.log(i);
}, i * 1000)
}

kavitaghatge
Автор

for(var i=0;i<4;i++)
{ let p=i; setTimeout(()=>console.log(p), 1000);
}
Soution of the problem ☝️☝️

AnujyotiDe-vflx