To check if a variable is a string in JavaScript, use the typeof
operator, i.e., if (typeof variable === 'string')
. If the typeof
variable returns 'string'
, then the variable is a string. Otherwise, it is not a string.
For example:
const variable = 'Coding Beauty';
if (typeof variable === 'string') {
// 👇 this runs
console.log('Variable is string');
} else {
console.log('Variable is not string');
}
The typeof
operator returns a string that indicates the type of its operand. For primitives, it returns the exact type, e.g., 'number'
, 'boolean'
, 'string'
, etc, for functions, it returns ‘function
‘, and for complex or user-defined types, it returns 'object'
.
console.log(typeof true); // 'boolean'
console.log(typeof 'Coding Beauty'); // 'string'
console.log(typeof 100); // 'number'
console.log(typeof new Promise(() => {})); // 'object'
console.log(typeof function () {}); // 'function'
console.log(typeof Symbol()); // 'symbol'
console.log(typeof (() => {})); // 'function'
console.log(typeof []); // 'object'
console.log(typeof {}); // 'object'
console.log(typeof undefined); // 'undefined'
typeof
undeclared_variable
is 'undefined'
?
Note that if you mistakenly use typeof
on a variable that hasn’t been declared, typeof
will not throw an error:
if (typeof undeclared === 'string') {
console.log('Variable is string');
} else {
console.log('Variable is NOT string');
}
When used on variables that haven’t been declared, typeof
returns the string 'undefined'
:
console.log(typeof undeclared); // 'undefined'
typeof
for string wrapper objects
The one instance where typeof
won’t work is when the string was created as a wrapper object i.e., using the String()
constructor with the new
operator.
For such strings, it will return the 'object'
string, instead of 'string'
.
const strObj = new String('Coding Beauty');
console.log(typeof strObj); // 'object'
However, it’s not a good practice to use wrapper objects, as they serve no use. Wrapper objects are only used internally; when JavaScript auto-boxes a primitive value into an object to access a property on that object.
typeof
null
is 'object'
?
Note that when typeof
is used on the null
value, it returns the string 'object'
console.log(typeof null) // 'object'
This is because, as stated in the MDN documentation:
In the first implementation of JavaScript, JavaScript values were represented as a type tag and a value. The type tag for objects was
0
.null
was represented as the NULL pointer (0x00
in most platforms). Consequently,null
had0
as type tag, hence thetypeof
return value"object"
. (reference)A fix was proposed for ECMAScript (via an opt-in), but was rejected. It would have resulted in
typeof null === "null"
.
typeof
NaN
is 'number'
?
Also, typeof NaN
results in 'number'
:
console.log(typeof NaN); // 'number'
Because as stated in this StackOverflow answer and the spec, NaN
is considered a numeric type in JavaScript.
Every Crazy Thing JavaScript Does
A captivating guide to the subtle caveats and lesser-known parts of JavaScript.