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 will always be false
.
function isNumber(x) {
return typeof x === "Number"; // Noncompliant: the function always returns 'false'
}
Instead, make sure you are always comparing the expression against one of the seven possible values.
function isNumber(x) {
return typeof x === "number";
}