Lodash isString: Types of Strings Validated

The _.isString function in the Lodash JavaScript library is designed to verify whether a given value qualifies as a string. This article covers the specific string types that _.isString evaluates as valid, explaining how it handles both primitive string literals and String wrapper objects, as well as detailing which common values fail the validation.

Primitive Strings

_.isString returns true for all standard primitive strings in JavaScript. This includes:

Primitive strings are values that have a typeof result equal to 'string'.

String Objects

Unlike a basic typeof value === 'string' check, _.isString also validates string instances created using the String object constructor.

In native JavaScript, running typeof new String('hello') yields 'object'. However, _.isString examines the internal object tag—checking that the value is an object-like structure with a [object String] tag. As a result, _.isString(new String('hello')) evaluates to true.

Values That Fail Validation

_.isString strictly checks the internal type definition and does not perform type coercion. The following inputs evaluate to false:

Summary Comparison

// Valid strings (return true)
_.isString('hello');             // true
_.isString("");                  // true
_.isString(`template`);          // true
_.isString(new String('hello')); // true

// Invalid values (return false)
_.isString(123);                 // false
_.isString(null);                // false
_.isString(undefined);           // false
_.isString(['hello']);           // false
_.isString({ a: 'hello' });      // false