
JavaScript Quiz Questions
- What is the output of the following code?
JavaScript
var x = 1;
var y = 2;
console.log(x + y);
Code snippet
Output: 3
- What is the output of the following code?
JavaScript
var x = “Hello”;
var y = “World”;
console.log(x + y);
Code snippet
Output: HelloWorld
- What is the output of the following code?
JavaScript
var x = 1;
var y = 2;
if (x > y) {
console.log(“x is greater than y”);
} else {
console.log(“y is greater than x”);
}
Code snippet
Output: y is greater than x
- What is the output of the following code?
JavaScript
var x = 1;
var y = 2;
while (x < y) {
x++;
}
console.log(x);
Code snippet
Output: 3
- What is the output of the following code?
JavaScript
var x = [1, 2, 3];
for (var i = 0; i < x.length; i++) {
console.log(x[i]);
}
Code snippet
Output: 1
2
3
- What is the output of the following code?
JavaScript
var x = “Hello”;
for (var i = 0; i < x.length; i++) {
console.log(x[i]);
}
Code snippet
Output: H
e
l
l
o
- What is the output of the following code?
JavaScript
var x = {
name: “John Doe”,
age: 30
};
console.log(x.name);
console.log(x.age);
Code snippet
Output: John Doe
30
- What is the output of the following code?
JavaScript
var x = {
name: “John Doe”,
age: 30
};
x.name = “Jane Doe”;
console.log(x.name);
Code snippet
Output: Jane Doe
- What is the output of the following code?
JavaScript
var x = {
name: “John Doe”,
age: 30
};
x.age = 31;
console.log(x.age);
Code snippet
Output: 31
- What is the output of the following code?
JavaScript
var x = {
name: “John Doe”,
age: 30
};
x.sayHello = function() {
console.log(“Hello, my name is ” + this.name);
};
x.sayHello();
Code snippet
Output: Hello, my name is John Doe
