Check if a Variable is a String using JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 3, 2024
4 min

banner

# Check if a Variable is a String in JavaScript

Use the typeof operator to check if a variable is a string, e.g. if (typeof variable === 'string').

If the typeof operator returns "string", then the variable stores a string.

index.js
const variable = 'bobbyhadz.com'; if (typeof variable === 'string') { // ๐Ÿ‘‡๏ธ this runs console.log('โœ… the value is of type string'); } else { console.log('โ›”๏ธ the value is NOT of type string'); }

check if variable is string

The code for this article is available on GitHub

We used the typeof operator to check if a variable is a string.

The operator returns a string that indicates the type of the value.

Here are some examples:

index.js
console.log(typeof ""); // ๐Ÿ‘‰๏ธ "string" console.log(typeof "hello"); // ๐Ÿ‘‰๏ธ "string" console.log(typeof "42"); // ๐Ÿ‘‰๏ธ "string" console.log(typeof function () {}); // ๐Ÿ‘‰๏ธ "function" console.log(typeof null); // ๐Ÿ‘‰๏ธ "object" console.log(typeof []); // ๐Ÿ‘‰๏ธ "object" console.log(typeof {}); // ๐Ÿ‘‰๏ธ "object" console.log(typeof 0); // ๐Ÿ‘‰๏ธ "number"

using the typeof operator

If the variable stores a string, the equality comparison will return true and the if block will run, otherwise, the else block will run.

The typeof operator doesn't throw an error even if you use it with a variable that has not been declared.
index.js
if (typeof variable === 'string') { console.log('โœ… type is string'); } else { // ๐Ÿ‘‡๏ธ this runs console.log('โ›”๏ธ type is NOT string'); }
The code for this article is available on GitHub

We used the typeof operator with a variable that hasn't been declared, so it returned a sting of "undefined".

index.js
console.log(typeof doesNotExist); // ๐Ÿ‘‰๏ธ "undefined"

# No automatic type conversion is performed

Note that no automatic type conversion is performed. If your variable contains a number that is wrapped in a string, the typeof operator still returns "string".

index.js
console.log(typeof '42'); // ๐Ÿ‘‰๏ธ string

no automatic type conversion is performed

The code for this article is available on GitHub

The only scenario where the typeof operator wouldn't detect a string is if the string is created using the new String() constructor.

index.js
const str = new String('hello world'); console.log(typeof str); // ๐Ÿ‘‰๏ธ "object"

Note that the String() constructor is not the same as the String object. The difference being the new keyword.

index.js
console.log(typeof new String('hello')); // ๐Ÿ‘‰๏ธ "object" console.log(typeof String('hello')); // ๐Ÿ‘‰๏ธ "string"

The String() constructor (with the new keyword), produces an instance of the String type and should never be used.

Whereas, the String object is used to convert a value to a string.

The typeof operator is all you need to check if a variable stores a string.

# Defining a reusable function

If you have to check if a variable stores a string often, define a reusable function.

index.js
function isString(value) { return typeof value === 'string'; } console.log(isString('abc')); // ๐Ÿ‘‰๏ธ true console.log(isString('')); // ๐Ÿ‘‰๏ธ true console.log(isString(42)); // ๐Ÿ‘‰๏ธ false console.log(isString([])); // ๐Ÿ‘‰๏ธ false console.log(isString(null)); // ๐Ÿ‘‰๏ธ false const variable = 'bobbyhadz.com'; if (isString(variable)) { // ๐Ÿ‘‡๏ธ this runs console.log('The variable stores a string'); } else { console.log('The variable does NOT store a string'); }

defining reusable function

The code for this article is available on GitHub

The isString() function takes a value as a parameter and returns true if the value is of type string and false otherwise.

# Check if a Variable is a String using lodash

First, make sure you have lodash installed by running the following command from your terminal.

shell
# ๐Ÿ‘‡๏ธ initialize package.json if you don't have one npm init -y npm install lodash

Now you can import and use the isString() method to check if a value is a string.

index.js
import _ from 'lodash'; console.log(_.isString('hello')); // ๐Ÿ‘‰๏ธ true console.log(_.isString('')); // ๐Ÿ‘‰๏ธ true console.log(_.isString(true)); // ๐Ÿ‘‰๏ธ false console.log(_.isString(false)); // ๐Ÿ‘‰๏ธ false console.log(_.isString(null)); // ๐Ÿ‘‰๏ธ false const variable = 'bobbyhadz.com'; if (_.isString(variable)) { // ๐Ÿ‘‡๏ธ this runs console.log('The variable is a string'); } else { console.log('The variable is NOT a string'); }

check if variable is string using lodash

The code for this article is available on GitHub

The lodash.isString() method returns true if the supplied value is a string and false otherwise.

Even if you already use lodash in your project, I would advise against using the isString() method as the typeof operator is sufficient and doesn't require you to use an external library.

# Check if a Variable is a String using Object.prototype.toString.call()

Alternatively, you can use the Object.prototype.toString.call() method.

index.js
const variable = 'bobbyhadz.com'; if ( Object.prototype.toString.call(variable) === '[object String]' ) { // ๐Ÿ‘‡๏ธ this runs console.log('The variable is a string'); } else { console.log('The variable is NOT a string'); }

check if variable is string using object prototype tostring call

The code for this article is available on GitHub

Here is an example of defining a reusable function.

index.js
function isString(value) { return ( Object.prototype.toString.call(value) === '[object String]' ); } console.log(isString('hello')); // ๐Ÿ‘‰๏ธ true console.log(isString('')); // ๐Ÿ‘‰๏ธ true console.log(isString(null)); // ๐Ÿ‘‰๏ธ false

The Object.prototype.toString.call() method can be used to stringify a value.

When a string is stringified, the string '[object String]' is returned.

You shouldn't have to use this approach as it relies on the language's internals and is unintuitive.

# Don't use the instanceof operator to check if a value is a string

You should never use the instanceof operator to check if a value is of type string.

index.js
// โ›”๏ธ BAD - don't do this function isString(value) { return value instanceof String; } console.log(isString('hello')); // ๐Ÿ‘‰๏ธ true console.log(isString('')); // ๐Ÿ‘‰๏ธ true console.log(isString(null)); // ๐Ÿ‘‰๏ธ false console.log(isString(new String('hello'))); // ๐Ÿ‘‰๏ธ true
The code for this article is available on GitHub

The syntax for the instanceof operator is object instanceof constructor.

The operator returns true if the prototype property of the constructor appears anywhere in the prototype chain of the object.

Notice that the operator returns true only for object-wrapped strings.

These are strings that were created with the new String() constructor.

You should never use the new String() constructor in your code, so you should never have to use the instanceof operator to check if a value is a string.

The typeof operator is all you need in order to check if a value stores a string in JavaScript.

index.js
const variable = 'bobbyhadz.com'; if (typeof variable === 'string') { // ๐Ÿ‘‡๏ธ this runs console.log('โœ… the value is of type string'); } else { console.log('โ›”๏ธ the value is NOT of type string'); }
The code for this article is available on GitHub

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev