typeof that returns what type of value a variable holds.
typeof operator can identify has expanded over time, setting the stage for more complex data type interactions in the process.
typeof operator keeps developers from making data typing mistakes.
- Null — Used when an object has no value.
- Undefined — Used when an object has a value, but the value is not defined.
- Boolean — True or false value.
- Number — Integer or floating point (decimal) number.
- String — Sequence of characters that represent text.
- Symbol — Anonymous, unique identifier. (Added in 2015.)
- BigInt — Special type of number that can be larger than the maximum value specified by the Number type and whose precision is only limited by computer memory. (Added in 2020.)
Developers distinguish each of these data types using the
typeof operator. For customized data types, which would always have a base data type of Object, the
let example1 = "1" + 2; // the text string "12" let example2 = 1 + 2; // the number 3
Unexpected behavior like this is common when combining data types, so it is important to make sure that all data are the same type before trying to combine them. In the previous example, to force the first expression to result in a number, you must convert it to a number first:
let example3a = "1"; let example3b = parseInt(example3a); // cast the string to a number let example3c = example3b + 2; // the number 3
let example4a; let example4b = null; // variable has a "null" value console.log(example4a); // printing undefined variables is unhelpful console.log(example4b); // printing null variables is just fine
- Null — typeof instance === “object”
- Undefined — typeof instance === “undefined”
- Boolean — typeof instance === “boolean”
- Number — typeof instance === “number”
- String — typeof instance === “string”
- Object — typeof instance === “object”
- Function — typeof instance === “function”
- Symbol — typeof instance === “symbol”
- BigInt — typeof instance === “bigint”
typeof response to “null,” but the idea has never been implemented. Reasons range from legacy code compatibility to this inconsistency just not being a major issue, just a small quirk developers need to remember.
typeof operator cannot detect customized data types or object names, only those built into the language.
typeof operator, and other programming concepts.