This JavaScript basics MCQ quiz has a pool of 25 questions on variables, data types and the difference between let, const and var. It is made for freshers and computer students preparing for their first JavaScript interview. Every attempt gives you 10 random questions from the pool. Answer them, press Submit, and you will see your score, which answers were right or wrong, and a short explanation for each one.
Most questions ask “What is the output of this code?”, because that is how interviewers usually test the basics. Take the quiz again to get a new set of 10, and find all 25 answers with explanations below the quiz.
Take the JavaScript Basics MCQ Quiz
What This Quiz Covers
Every question comes from three topics that appear in almost every JavaScript fresher interview:
- Variables: how to declare them, naming rules, and what an uninitialised variable holds.
- Data types: the 7 primitive types, objects, and the
typeofoperator, including its famous surprises. - let, const and var: scope, hoisting, the temporal dead zone, redeclaring and reassigning.
JavaScript has 7 primitive data types: string, number, bigint, boolean, undefined, symbol and null. Everything else, including arrays and functions, is an object. Keep this list in mind, because several questions in the quiz depend on it.
Difference Between var, let and const
Most mistakes in a JavaScript basics MCQ test come from mixing up these three keywords. This table shows the four differences interviewers ask about.
| Feature | var | let | const |
|---|---|---|---|
| Scope | Function | Block | Block |
| Can be reassigned | Yes | Yes | No |
| Can be redeclared in the same scope | Yes | No | No |
| Use before the declaration line | Gives undefined | ReferenceError | ReferenceError |
| Must be given a value when declared | No | No | Yes |
A simple rule for real projects: use const by default, use let when the value has to change, and avoid var in new code. You can read the full rules for let on MDN, the standard JavaScript reference.
JavaScript Basics MCQ Answers with Explanations
Try the quiz first. Then use this section to check all 25 answers and understand why each one is correct. The quiz shows the options in a random order, so the answers here are written out, not given as A, B, C or D.
Q1. Which keyword declares a block-scoped variable that can be reassigned later?
Answer: let. let is block-scoped and its value can change. const is also block-scoped but cannot be reassigned, and var is function-scoped.
Q2. What is the output of this code?
console.log(typeof null);Answer: “object”. null is a primitive, but typeof null returns "object". This is a bug from the first version of JavaScript that was never fixed, so old code would not break.
Q3. What is the output of this code?
console.log(typeof NaN);Answer: “number”. NaN means “Not a Number”, but it is a special value of the number type. You get it from failed maths such as 0 / 0 or Number("abc").
Q4. How many primitive data types does JavaScript have?
Answer: 7. They are string, number, bigint, boolean, undefined, symbol and null. Object is the only non-primitive type.
Q5. What is the output of this code?
console.log(x);
var x = 5;Answer: undefined. var declarations are hoisted to the top of their scope and set to undefined. The value 5 is assigned only when the second line runs.
Q6. What is the output of this code?
console.log(y);
let y = 5;Answer: ReferenceError. let is hoisted too, but it is not initialised. From the start of the block until the declaration line, the variable is in the “temporal dead zone”, and reading it throws a ReferenceError.
Q7. What is the output of this code?
const user = { age: 20 };
user.age = 21;
console.log(user.age);Answer: 21. const stops you from pointing user at a new value, but the object itself can still change. To stop changes to the object, use Object.freeze().
Q8. What happens when this code runs?
const n = 10;
n = 20;Answer: TypeError. Assigning a new value to a const variable throws “TypeError: Assignment to constant variable.”
Q9. What is the output of this code?
if (true) {
var a = 1;
let b = 2;
}
console.log(typeof a, typeof b);Answer: number undefined. var a ignores the block, so it is visible outside. let b exists only inside the block, and typeof on a name that does not exist returns "undefined" instead of throwing an error.
Q10. What is the output of this code?
console.log(typeof []);Answer: “object”. Arrays are objects in JavaScript. To check for an array, use Array.isArray([]), which returns true.
Q11. What is the output of this code?
console.log(typeof 10n);Answer: “bigint”. The n at the end makes 10n a BigInt, a separate primitive type for whole numbers larger than a normal number can hold safely (above 253 – 1). Without the n, typeof 10 would be "number".
Q12. In a normal browser script, which top-level declaration creates a property on the window object?
Answer: var. A top-level var name becomes window.name. Top-level let and const are global too, but they are not added to window.
Q13. What happens when this code runs?
let city = "Pune";
let city = "Mumbai";Answer: SyntaxError. A let variable cannot be declared twice in the same scope. The error is raised before any code runs: “Identifier ‘city’ has already been declared.” With var, the same code would work.
Q14. What is the output of this code?
let score;
console.log(score);Answer: undefined. A let or var variable declared without a value holds undefined. A const must always be given a value, so const score; is a SyntaxError.
Q15. Which of these is a valid variable name in JavaScript?
Answer: _total. A name can start with a letter, _ or $, but not a digit, so 2total is invalid. my-total is read as “my minus total”, and class is a reserved word.
Q16. What is the output of this code?
console.log(typeof function () {});Answer: “function”. Functions are objects, but typeof gives them their own answer, "function". This is the only object for which typeof does not return "object".
Q17. What is the output of this code?
let x = "10";
let y = Number(x);
console.log(typeof y);Answer: “number”. Number() converts a value to the number type, so "10" becomes 10. Use it whenever you read numbers from a form field, because input values are always strings.
Q18. Which of these is NOT a primitive data type in JavaScript?
Answer: Array. An array is an object. Symbol, BigInt and Boolean are all primitive types.
Q19. What is the output of this code?
console.log(typeof typeof 1);Answer: “string”. typeof 1 returns the text "number", and the type of any text is "string".
Q20. What is the output of this code?
let name = "Asha";
let Name = "Ravi";
console.log(name);Answer: Asha. JavaScript is case-sensitive, so name and Name are two different variables.
Q21. What is the output of this code?
var a = 1;
var a = 2;
console.log(a);Answer: 2. var allows the same variable to be declared again in the same scope, and the second value replaces the first. The same code with let would be a SyntaxError.
Q22. What is the output of this code?
function test() {
var msg = "hi";
}
test();
console.log(typeof msg);Answer: “undefined”. var is function-scoped, so msg exists only inside test(). Outside the function the name does not exist, and typeof returns "undefined".
Q23. What happens when this code runs?
const PI;Answer: SyntaxError. A const must be given its value in the same line: “Missing initializer in const declaration.” Write const PI = 3.14; instead.
Q24. What is the output of this code?
let a = { n: 1 };
let b = a;
b.n = 2;
console.log(a.n);Answer: 2. Objects are copied by reference. a and b point to the same object, so changing it through b also changes what a sees.
Q25. What is the output of this code?
let x = 5;
let y = x;
y = 10;
console.log(x);Answer: 5. Primitive values are copied by value. y gets its own copy of 5, so changing y does not touch x.
How to Score Better Next Time
- Run every code question yourself in the browser console (press F12 and open the Console tab).
- Remember the two
typeofsurprises:typeof nullis “object” andtypeof NaNis “number”. - Learn the var, let and const table above by heart. It answers many of the questions in this quiz.
If you are also preparing web basics for the same interview, try our HTML MCQ questions, and if your company asks backend questions too, practise with the PHP MCQ questions with answers.
Frequently Asked Questions
Is this JavaScript quiz good for freshers?
Yes. It covers only the basics that every fresher JavaScript interview starts with: variables, data types and let, const and var. You do not need to know functions or the DOM for this quiz.
Should I use var, let or const?
Use const by default and let when the value must change. Avoid var in new code, because its function scope and hoisting cause bugs that let and const prevent.
Why does typeof null return “object”?
It is a bug from the first version of JavaScript in 1995. Fixing it would break existing websites, so it was kept. null is still a primitive type.
Conclusion
This JavaScript basics MCQ quiz tested the three topics that start almost every JavaScript interview: variables, data types and the difference between var, let and const. If you scored 8 or more out of 10, retake the quiz once or twice to see new questions from the pool of 25. If not, read the explanations again, run the code in your browser console, and try again.
