Lodash _.isString() method
What you’ll learn
- How
_.isString(value)accepts both string primitives and boxedStringobjects. - Why this is broader than a bare
typeof === "string"check. - How empty strings still count as valid strings.
- Where to combine this guard with a length or pattern check.
Prerequisites
You can tell a string primitive from a boxed wrapper such as new String("hello").
- You have used template literals and traditional string quotes.
- Try-it labs load lodash from the CDN.
Overview
Use _.isString before string-only operations—trimming, splitting, regex matching, or template interpolation—so accidental numbers, nulls, or arrays don’t crash your call chain.
Primitive + boxed
Accepts "abc" and new String("abc").
Empty still counts
"" is a string. Use length if you need non-empty.
Cross-realm safe
Tag check survives boundary cases instanceof String misses.
Syntax
_.isString(value) - value: any value to test.
- Returns:
truewhen value is a String primitive or a boxed String object; otherwisefalse.
String primitives
The lodash docs baseline: literals and template strings always return true.
import isString from "lodash/isString";
console.log(
"abc: " + isString("abc") + "\n" + // true (lodash docs)
"template: " + isString(`Hello, world!`) + "\n" + // true
"empty: " + isString("") // true
); Boxed new String() objects
Lodash sees the internal [object String] tag, so wrappers also pass—unlike a bare typeof.
import isString from "lodash/isString";
const boxed = new String("boxed");
console.log(
"boxed: " + isString(boxed) + "\n" + // true
"typeofRaw: " + (typeof boxed) // "object"
); Numbers, arrays, and nullish
Other types never qualify—the lodash docs example shows _.isString(1) as false.
import isString from "lodash/isString";
console.log(
"num: " + isString(1) + "\n" + // false (lodash docs)
"arr: " + isString(["a", "b"]) + "\n" + // false
"obj: " + isString({ text: "x" }) + "\n" + // false
"nullVal: " + isString(null) // false
); 📋 _.isString vs related checks
| API / pattern | Behavior |
|---|---|
_.isString(x) | true for both string primitives and boxed String objects. |
typeof x === "string" | Primitive only—misses new String(…). |
x instanceof String | Only the boxed object form; misses primitives. |
Object.prototype.toString.call(x) | Manual tag check—lodash wraps the same idea. |
Pitfalls to avoid
Empty vs missing
_.isString("") is true—don’t rely on truthiness to detect blank inputs.
No auto-conversion
A numeric 42 never passes; call String(value) or template-tag it yourself.
Boxed objects break typeof
A wrapped new String("x") is typeof object; _.isString still returns true.
❓ FAQ
Summary
- Purpose: confirm a value is a String primitive or boxed wrapper before string operations.
- Remember: empty
""still counts; numbers and arrays do not. - Next: explore more on Lodash _.isSymbol().
_.isString accepts both "abc" and new String("abc") because lodash checks the internal [object String] tag in addition to typeof—typeof new String("x") alone returns "object".
6 people found this page helpful
