- Published on
20+ Output questions in javascript

20+ Output questions in javascript
JavaScript Output Questions Explained
1.What is the output of the following JavaScript code?
console.log(1 + "2" + "2");
console.log(1 + +"2" + "2");
console.log(1 + -"1" + "2");
console.log(+"1" + "1" + "2");
console.log("A" - "B" + "2");
console.log("A" - "B" + 2);
The output of the code will be:
122;
32;
02;
112;
NaN2;
NaN;
Explanation
In the first line, the +
operator is used to concatenate the string "2" to the number 1, so the result is the string "12". In the second line, the +
operator is used twice: first to convert the string "2" to a number, and then to add that number to 1, so the result is the number 3. In the third line, the -
operator is used to convert the string "1" to a number and then subtract that number from 1, so the result is the number 0. In the fourth line, the +
operator is used twice: first to convert the string "1" to a number, and then to add that number to the string "1", so the result is the string "11".
In the fifth and sixth lines, the -
operator is used to subtract the string "B" from the string "A", which is not a valid operation and therefore produces the special value NaN
(not a number). When the NaN
value is concatenated with the string "2" in the fifth line, the result is the string "NaN2". When it is added to the number 2 in the sixth line, the result is still NaN
.
2. What is the output of the following code, and why?
console.log(typeof null);
The output of the code will be:
"object";
//Because in JavaScript, the data type of null is an object
3. What is the output of the following code, and why?
const array = [1, 2, 3];
for (let i = 0; i < array.length; i++) {
setTimeout(() => {
console.log(array[i]);
}, 1000);
}
The output of the code will be:
3 3 3
// refer how async js work
4. What is the output of the following code, and why?
const foo = {
bar: function () {
console.log(this);
},
};
foo.bar();
const baz = foo.bar;
baz();
5. What is the output of the following code, and why?
const x = 10;
const y = "10";
console.log(x == y);
console.log(x === y);
6. What is the output of the following JavaScript code?
console.log(0.1 + 0.2);
7. What is the result of running the following code?
let x = 10;
let y = "20";
console.log(x + y);
8. Consider the following code:
function greet(name) {
console.log("Hello " + name);
}
greet("John");
What is the output of this code?
9. What is the output of the following code?
const arr = [1, 2, 3];
console.log(arr[3]);
10. Consider the following code:
let x = 10;
let y = 20;
console.log(x > y);
What is the output of this code?
11. What is the output of the following JavaScript code?
async function foo() {
console.log("Hello");
return "World";
}
async function bar() {
const result = await foo();
console.log(result);
}
bar();
The output of this code would be:
Hello;
World;
12. What is the result of running the following code?
async function foo() {
return Promise.resolve("Hello");
}
async function bar() {
const result = await foo();
console.log(result);
}
bar();
The output of this code would be:
Hello;
13. Consider the following code:
async function foo() {
return Promise.reject("Error");
}
async function bar() {
try {
const result = await foo();
console.log(result);
} catch (error) {
console.log(error);
}
}
bar();
What is the output of this code?
The output of this code would be:
Error;
14. What is the output of the following code?
async function foo() {
return "Hello";
}
async function bar() {
const result = await foo();
console.log(result);
}
const result = bar();
console.log(result);
The output of this code would be:
Hello
Promise { "Hello" }
15. Consider the following code:
async function foo() {
return "Hello";
}
async function bar() {
const result = await foo();
console.log(result);
}
bar()
.then((result) => console.log(result))
.catch((error) => console.log(error));
16.What is the output of this code?
The output of this code would be:
Hello;
Hello;