JavaScript Interview Questions
Here are some interesting JavaScript interview questions.
function x() {
var a = 10;
setTimeout(function() {
console.log("Hello");
}, 1000);
console.log(a);
}
x();
Output
10
Hello[After 1 sec]
Time, tide and JavaScript wait for none.
Problem Statement: Print numbers from 1 to 5 such that 1 appears in the console after 1 sec, 2 after 2 secs, and so on..
var num = 0;
function printNum() {
if(num === 6)
return;
if(num > 0)
console.log(num);
num++;
setTimeout(printNum , num * 1000);
}
printNum();
We are calling the function post the required time which gives us the desired output.
Complete list can be found here.