How to check if a variable is a string in JavaScript?

To check if a variable is a string in JavaScript, you can use the following methods, depending on whether you need to handle string primitives, String objects, or both. Here’s a detailed breakdown:

1. Using typeof (For Primitive Strings Only)

The simplest method for checking primitive strings (e.g., "hello"):

const isString = (variable) => typeof variable === 'string';

Example:

console.log(isString("hello")); // true
console.log(isString(new String("hello"))); // false (String object)
console.log(isString(123)); // false

Pros: Simple and fast.
Cons: Fails for String objects created with new String().

2. Using Object.prototype.toString (Primitives + Objects)

Detects both primitive strings and String objects:

const isString = (variable) => {
  return Object.prototype.toString.call(variable) === '[object String]';
};

Example:

console.log(isString("hello")); // true (primitive)
console.log(isString(new String("hello"))); // true (String object)
console.log(isString(123)); // false

Pros: Works for all string types.
Cons: Slightly more verbose.

3. Using instanceof (For String Objects Only)

Checks if a variable is an instance of String (only for objects):

const isStringObject = (variable) => variable instanceof String;

Example:

console.log(isStringObject("hello")); // false (primitive)
console.log(isStringObject(new String("hello"))); // true

Pros: Explicitly checks for String objects.
Cons: Fails for primitive strings.

4. Using Lodash’s _.isString

If you use Lodash, this method handles both primitives and objects:

import _ from 'lodash';

console.log(_.isString("hello")); // true
console.log(_.isString(new String("hello"))); // true

Key Considerations

  • Null/Undefined: Always check if the variable exists first to avoid errors:
  const isStringSafe = (variable) => {
    return variable !== null && typeof variable === 'string';
  };
  • String Objects: Rarely used in practice, but if your code uses new String(), use Object.prototype.toString or Lodash.
  • Edge Cases:
  Object.prototype.toString.call(null); // "[object Null]"
  Object.prototype.toString.call(undefined); // "[object Undefined]"

Method Comparison Table

MethodPrimitives ("hello")String Objects (new String())Notes
typeofSimple and fast.
Object.prototype.toStringWorks for all types.
instanceofOnly for objects.
Lodash _.isStringRequires Lodash.

Recommended Solution

Use Object.prototype.toString for universal checks:

const isString = (variable) => {
  return Object.prototype.toString.call(variable) === '[object String]';
};

For most use cases (primitive-only strings), typeof is sufficient:

if (typeof variable === 'string') {
  // Handle primitive string
}

Examples

// Primitive string
console.log(isString("hello")); // true

// String object
console.log(isString(new String("world"))); // true

// Non-strings
console.log(isString(42)); // false
console.log(isString(null)); // false
console.log(isString({})); // false

Leave a Reply

Your email address will not be published. Required fields are marked *