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:
- Single-quoted strings:
'hello' - Double-quoted strings:
"world" - Template literals:
`template string` - Empty strings:
""or''
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.
- Explicit string wrappers:
new String('hello')
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:
- Numbers and Booleans: Values such as
123,NaN, ortrue. - Null and Undefined: Neither
nullnorundefinedare strings. - Objects and Arrays: Arrays like
['text']or plain objects like{ text: 'hello' }. - Custom Objects with
toString: Even if an object defines a customtoString()method,_.isStringreturnsfalsebecause the object itself is not a native string or aStringinstance. - Symbols: Values created via
Symbol('text').
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