How to check if a variable is a string in JavaScript (with examples)

Updated Sep 22, 2023 ⤳ 5 min read

When working with strings in JavaScript, it’s important to check if a variable is a string before performing any string-related operations – specially when you get the value from an API . This quick guide explores four methods for checking if a variable is a string in JavaScript.

🎧 Debugging Jam

Calling all coders in need of a rhythm boost! Tune in to our 24/7 Lofi Coding Radio on YouTube, and let's code to the beat – subscribe for the ultimate coding groove!" Let the bug-hunting begin! 🎵💻🚀

24/7 lofi music radio banner, showing a young man working at his computer on a rainy autmn night with hot drink on the desk.

JavaScript is a dynamically typed language meaning the data type of a variable can change at runtime. That said, it’s essential to determine the data type of a variable before performing any operation on it, and string values are no exception.

How to check if a variable is a string

In this section, we’ll explore four common approaches to checking whether a value is a string in JavaScript:

  1. Using typeof
  2. Using Object.prototype.constructor.name
  3. Using Object.prototype.toString.call(obj)
  4. Using instanceof

1. Using typeofThe typeof operator is the go-to method when testing data types. It simply returns a string value representing the data type of its operand. For instance, typeof "hello" returns "string"

This is how you can use typeof to test if a variable is a string:


let str = 'hello';

if (typeof str === 'string') {
  console.log('str is a string');
}

In the above example, the variable str contains the string value hello, and the value returned by typeof confirms that.

The typeof operator only works with string literals, though. If your code, for some reason, contains string objects, you need to add more checks (more on this below).

2. Using Object.prototype.constructor.name: Every JavaScript object has a constructor property that refers to the constructor function that created it (e.g., str.constructor). 

This constructor has an attribute called name, which returns the function name (as a string) that created the respective object. 

In the case of string values, the function name is String. This work well with string literals as well as string objects:


let str = 'hello';

if (str.constructor.name === 'String') {
  console.log('str is a string');
}

3. Using Object.prototype.toString.call(obj):  This method can be considered a detailed version of typeof because it returns the object type name when given an object.

The returned value is usually in the form of [object Type] where Type can be Array, String, Number, Function, etc.

Let's try it out with a string:


let str = 'hello';

if (Object.prototype.toString.call(str) === '[object String]') {
  console.log('str is a string');
}

Just like Object.prototype.constructoryou can use this approach for both string literals and objects.

4. Using instanceof: This approach only works with string objects (e.g., new String('test')).

The instanceof checks if its first operand is an instance of its second operand. Or more precisely, it checks if an object's prototype chain includes the prototype property of a constructor.


let a = new String('test')

console.log(a instanceof String)
// Output true

Please note there is a difference between primitive strings (a.k.a string literals) and String objects.

A string literal (wrapped in single/double quotation marks) is a primitive value. String values created via calling the String function without using the new keyword are primitive strings too.


let a = 'test'
let b = String('test')
let c = new String('test')

console.log(a === b)
// Output: true

console.log(a === c)
// Output: false

console.log(b === c)
// Output: false

That said, instanceof doesn't work with string literals.

Defining a string value by using the String as a constructor (new String('text')) is discouraged by many developers, as it's almost useless. You should rarely find yourself using String as a constructor.

Then, why should I even consider this approach? You may ask.

Well, there's one scenario you might want to use instanceof to check strings in JavaScript, and that's when you need to create a universal utility function that works with all string types - no matter how they are defined:


function isString(value) {
return typeof value === 'string' || value instanceof String
}

You can use constructor.name, or toString.call(obj) to achieve the same result.

Wrapping up

In this article, we have discussed four different approaches to checking if a variable is a string in JavaScript. The most common approach is to use typeof to test if the value is string. And if your code contains both string literals and objects, you can use typeof in conjunction with instanceof.

Alternatively, you can use constructor.name, or toString.call(obj) to get the class constructor name that created the string value.

Alright! I think it does it for today! I hope you found this quick guide helpful.

Thanks for reading.

Disclaimer: This post may contain affiliate links. I might receive a commission if a purchase is made. However, it doesn’t change the cost you’ll pay.

`