JavaScript MCQ Questions with Answer
Categories: MCQS
JavaScript MCQ Questions with Answer
1. What is the correct way to declare a variable in JavaScript?
A) var variableName;
B) variable variableName;
C) let variableName;
D) const variableName;
Answer: C) let variableName;
2. What is the output of the following code?
console.log(typeof(42));
A) "number"
B) "string"
C) "boolean"
D) "undefined"
Answer: A) "number"
3. Which of the following is not a JavaScript data type?
A) number
B) boolean
C) array
D) date
Answer: C) array
4. What is the output of the following code?
console.log("Hello" + 42);
A) "Hello"
B) "42"
C) "Hello42"
D) undefined
Answer: C) "Hello42"
5. Which of the following is the correct way to create a function in JavaScript?
A) function myFunction() {}
B) myFunction = function() {}
C) const myFunction = () => {}
D) All of the above are correct
Answer: D) All of the above are correct
6. What is the output of the following code?
const x = 5;
console.log(x == "5");
A) true
B) false
C) undefined
D) null
Answer: A) true
7. What does the "=== " operator do in JavaScript?
A) compares the value of two variables
B) compares the type and value of two variables
C) assigns a value to a variable
D) checks if a variable is defined
Answer: B) compares the type and value of two variables
8. What is the output of the following code?
let i = 0;
while (i < 5) {
console.log(i);
i++;
}
A) 0, 1, 2, 3, 4
B) 1, 2, 3, 4, 5
C) 0, 1, 2, 3, 4, 5
D) undefined
Answer: A) 0, 1, 2, 3, 4
9. What is the output of the following code?
const arr = [1, 2, 3, 4];
console.log(arr[2]);
A) 1
B) 2
C) 3
D) 4
Answer: C) 3
10. What is the output of the following code?
const myObject = {
name: "John",
age: 30,
city: "New York"
};
console.log(myObject.age);
A) "John"
B) 30
C) "New York"
D) undefined
Answer: B) 30