What does the typeof operator return for an undefined variable?
a) “undefined”
b) “null”
c) “object”
d) “number”
Answer:
a) “undefined”
Explanation:
The typeof
operator in JavaScript returns the string “undefined” when used on a variable that has not been assigned a value. This is different from null, which is an object.
For example:
let x;
console.log(typeof x); // Output: "undefined"
This helps in checking if a variable has been initialized before performing operations on it.