Javascript : Guess the Output
Question
x = 23;
const x;
Question
x = 23;
let x;
Question
let x;
const y;
var z;
console.log(x);
console.log(y);
console.log(z);
Question
console.log(1);
setTimeout(function () {
console.log(2);
}, 1000);
setTimeout(function () {
console.log(3);
}, 0);
console.log(4);
Question
function x() {
setTimeout(function () {
console.log(i);
}, 1000);
var i = 1;
}
x();
Question
function x() {
setTimeout(function () {
console.log(i);
}, 1000);
let i = 1;
}
x();
Question
function x() {
setTimeout(function () {
console.log(i);
}, 1000);
i = 1;
let i;
}
x();
Question
(function () {
setTimeout(() => console.log(1), 2000);
console.log(2);
setTimeout(() => console.log(3), 0);
console.log(4);
})();
Question
function func1() {
setTimeout(() => {
console.log(x);
console.log(y);
}, 3000);
var x = 12;
let y = 20;
}
func1();
Question
function func2() {
for (var i = 0; i < 3; i++) {
setTimeout(() => console.log(i), 2000);
}
}
func2();
Question
function func2() {
for (let i = 0; i < 3; i++) {
setTimeout(() => console.log(i), 2000);
}
}
func2();
Question
let x = {},
y = { name: "Ronny" },
z = { name: "John" };
x[y] = { name: "Vivek" };
x[z] = { name: "Akki" };
console.log(x[y]);
Question
function runFunc() {
console.log("1" + 1);
console.log("A" - 1);
console.log(2 + "-2" + "2");
console.log("Hello" - "World" + 78);
console.log("Hello" + "78");
}
runFunc();
Question
let a = 0;
let b = false;
console.log(a == b);
console.log(a === b);
Question
var x = 23;
(function () {
var x = 43;
(function random() {
x++;
console.log(x);
var x = 21;
})();
})();
Question
let hero = {
powerLevel: 99,
getPower() {
return this.powerLevel;
},
};
console.log(hero.getPower());
let getPower = hero.getPower;
let hero2 = { powerLevel: 42 };
console.log(getPower());
console.log(getPower.apply(hero2));
Question
const a = function () {
console.log("a");
console.log(this);
const b = {
func1: function () {
console.log("b");
console.log(this);
},
};
const c = {
func2: () => {
console.log("c");
console.log(this);
},
};
b.func1();
c.func2();
};
a();
Question
const b = {
name: "Vivek",
f: function () {
var self = this;
console.log(this.name);
(function () {
console.log(this.name);
console.log(self.name);
})();
},
};
b.f();
Each time bigFunc is called, an array of size 700 is being created, Modify the code so that we don't create the same array again and again.
function bigFunc(element) {
let newArray = new Array(700).fill("♥");
return newArray[element];
}
console.log(bigFunc(599)); // Array is created
console.log(bigFunc(670)); // Array is created again
Question
(function (a) {
return (function () {
console.log(a);
a = 23;
})();
})(45);
The following code outputs 2 and 2 after waiting for one second. Modify the code to output 0 and 1 after one second.
function randomFunc() {
for (var i = 0; i < 2; i++) {
setTimeout(() => console.log(i), 1000);
}
}
randomFunc();
We have to store the name of every item in an array. What will be your approach?
[
{
name: "Menu 1",
link: "https://google.com",
subitems: [
{
name: "Menu 2",
link: "https://google.com",
},
],
},
{
name: "Menu 3",
link: "https://google.com",
subitems: [
{
name: "Menu 4",
link: "https://google.com",
subitems: [
{
name: "Menu 5",
link: "https://google.com",
},
{
name: "Menu 6",
link: "https://google.com",
},
],
},
],
},
];
Question
Question
Last updated