The typeof
operator returns a string indicating the type of its argument, and the set of returned values is limited:
- "undefined"
- "boolean"
- "number"
- "string"
- "symbol" (since ECMAScript 2015)
- "function"
- "object" (for
null
and any other object)
- "BigInt" (since ECMAScript 2020)
Compare a typeof
expression to anything else, and the result is predefined: false
.
Noncompliant Code Example
function someFunc(x) {
return typeof x === "Number"; // Noncompliant, function will always return 'false'
}
Compliant Solution
function someFunc(x) {
return typeof x === "number";
}