Master JavaScript

What typeof would say

What typeof would say

You are building a small teaching tool that sits beside a code editor. The reader types a value, and the tool shows what typeof would answer for it, so they can check their prediction without running anything.

The input is the value as it was typed, so it arrives as text: the four characters null, or the three characters 42n, or the seven characters "hello" including its quotation marks. Your job is to answer with the word typeof would produce.

The rules, in this order

  1. Exactly null gives "object". That is the famous lie, and the tool has to reproduce it.
  2. Exactly undefined gives "undefined".
  3. Exactly true or false gives "boolean".
  4. Exactly NaN or Infinity gives "number".
  5. Digits followed by a single n, such as 42n, gives "bigint".
  6. Digits, optionally with one dot and more digits, such as 42 or 3.5, gives "number".
  7. Anything starting with Symbol( gives "symbol".
  8. Anything starting with a double or single quotation mark gives "string".
  9. Anything starting with the word function gives "function".
  10. Anything starting with [ or { gives "object".
  11. Anything else gives "unknown".

The order matters. null starts with none of the special characters and would otherwise fall through to "unknown", so the exact matches have to be tried first.

Worked example

Input: "42n"

  rule 1  is it exactly null?        no
  rule 2  is it exactly undefined?   no
  rule 3  true or false?             no
  rule 4  NaN or Infinity?           no
  rule 5  digits then a single n?    yes

Output: "bigint"
Walking the rules for one input.

Constraints

  • literal is between 1 and 60 characters and has no spaces at either end.
  • Exactly one of the eleven rules applies, and the first one that matches wins.
  • The answer is always one of: "object", "undefined", "boolean", "number", "bigint", "string", "symbol", "function", "unknown".
Code

Run your code to see how it does on the examples. Submit also runs the hidden cases.