JavaScript typeof

Select one option

1 / 10

console.log(typeof typeof 10)

TL;DR answer sheet

JavaScript typeof — quiz answer sheet, click to expand.

1. console.log(typeof typeof 10)

Answer: string

Explanation: `typeof 10` returns "number" as a string, so you're actually doing `typeof "number"`, which is "string".

2. console.log(typeof /douiri/)

Answer: object

Explanation: Regular expressions in JavaScript are objects.

3. console.log(typeof 10n)

Answer: bigint

Explanation: The `n` suffix denotes a BigInt, which are numbers too high or too low to be represented by the number primitive.

4. console.log(typeof undeclaredVariable)

Answer: undefined

Explanation: Accessing an undeclared variable with `typeof` doesn't throw an error — it returns "undefined".

5. console.log(typeof [])

Answer: object

Explanation: Arrays are objects in JavaScript. To check if something is an array, use `Array.isArray()`.

6. console.log(typeof class C {})

Answer: function

Explanation: Classes in JavaScript are just syntactic sugar for constructor functions under the hood.

7. console.log(typeof null)

Answer: object

Explanation: This is a well-known JavaScript bug. `typeof null` returns "object" even though null isn't an object.

8. console.log(typeof NaN)

Answer: number

Explanation: `NaN` stands for Not-a-Number, but ironically, `typeof NaN` is "number".

9. console.log(typeof new Date())

Answer: object

Explanation: Dates are objects too, to check if something is a Date, use `instanceof Date`.

10. console.log(typeof Symbol("id"))

Answer: symbol

Explanation: Symbols are a unique primitive type in JavaScript.

Key Takeaways of this quiz

  • The `typeof` operator returns a string indicating the type of the operand.
  • `typeof null` returns "object", a well known javascript bug.
  • `typeof` on undeclared variables returns "undefined" — it doesn't throw an error.
  • `typeof []` returns "object", not "array" — use `Array.isArray()` to check for arrays.
  • `typeof class Foo {}` returns "function" — classes are just special functions.
  • BigInt and Symbol are modern primitive types: `typeof 10n` is "bigint" and `typeof Symbol()` is "symbol".
  • `typeof typeof x` always returns "string" because the inner `typeof` returns a string.
  • Regular expressions like `/abc/` are objects.
  • `typeof NaN` is "number", even though it's 'Not a Number'.
  • `typeof new Date()` is "object" — dates are objects like arrays and regex.

Similar Quizzes