How to Check If a Variable is a String in JavaScript [6 Ways]

Here are the six ways to check if a variable is a string in JavaScript:

  1. Using the typeof operator
  2. Using Object.prototype.toString.call() function
  3. Using Lodash/Underscore Library
  4. Using an instanceof operator
  5. Using constructor property
  6. Using Array.includes() method

Method 1: Using typeof operator

The typeof operator returns a string representing the type of the given variable. If the variable is a string, the typeof operator returns “string”.

const stringVariable = "Harry Potter Web Series";
const nonStringVariable = 42;

if (typeof stringVariable === "string") {
  console.log("stringVariable is a string");
} else {
  console.log("stringVariable is not a string");
}

if (typeof nonStringVariable === "string") {
  console.log("nonStringVariable is a string");
} else {
  console.log("nonStringVariable is not a string");
}

Output

stringVariable is a string

nonStringVariable is not a string

In this example, the code checks if stringVariable and nonStringVariable are strings by comparing the result of typeof to the string “string”. The check returns true for stringVariable and false for nonStringVariable.

Method 2: Using Object.prototype.toString.call() function

In JavaScript, the Object.prototype.toString.call() method is a more reliable way to determine the type of an object, especially when it comes to built-in types. This is because it returns a string in the form “[object Type]”, where “Type” is the internal class property of the object.

function isString(value) {
  return Object.prototype.toString.call(value) === '[object String]';
}

// Testing
console.log(isString("hello"));
console.log(isString(new String("hello")));
console.log(isString(123));
console.log(isString([]));

Output

Using Object.prototype.toString.call() function

Method 3: Using Lodash/Underscore Library

const _ = require('lodash')

console.log(_.isString("hello"));
console.log(_.isString(new String("hello")));
console.log(_.isString(123));
console.log(_.isString([]));

Output

true
true
false
false

Method 4: Using an instanceof operator

The instanceof operator checks if the variable is an instance of the String constructor. It will return true for String objects but not for string literals.

function isString(value) {
  return value instanceof String;
}

// Testing
console.log(isString("hello"));
console.log(isString(new String("hello")));
console.log(isString(123));
console.log(isString([]));

Output

false
true
false
false

The instanceof operator will only return true for String objects.

Method 5: Using constructor Property

This checks the constructor of the value to determine if it’s a String.

function isString(value) {
  return value.constructor === String;
}

// Testing
console.log(isString("hello"));
console.log(isString(new String("hello")));
console.log(isString(123));
console.log(isString([]));

Output

true
true
false
false

The constructor method will only return true for String objects.

Method 6: Using Array includes Method

This is a less common approach, but it’s interesting to note. It checks if the value is among the string representations of the value.

function isString(value) {
  return ["[object String]", "string"]
    .includes(Object.prototype.toString.call(value)
    || typeof value);
}

// Testing
console.log(isString("hello"));
console.log(isString(new String("hello")));
console.log(isString(123));
console.log(isString([]));

Output

true
true
false
false

Conclusion

It’s important to note that not all of these methods treat string literals and String objects similarly. The typeof operator, Object.prototype.toString.call(), and Lodash/Underscore methods will return true for both. However, the instanceof and constructor methods will only return true for String objects.

Choose the method that best fits the specifics of your situation and the types of string values (literals or objects) you expect to encounter. FThetypeof operator or the Object.prototype.toString.call() method should suffice. for most general use cases

Related posts

How to Check If Variable Exists in JavaScript

How to Check If a Variable is an Array in JavaScript

How to Check Null Value in JavaScript

How to Check for undefined in JavaScript