The value to check
True if the value is a String object, otherwise false
isStringObject(new String("")); // true
isStringObject(new String("abc")); // true
isStringObject(Object("abc")); // true
isStringObject("abc"); // false (a primitive string, see isString)
isStringObject(String("abc")); // false (String() without new returns a primitive string)
isStringObject(null); // false
isStringObject(undefined); // false
isStringObject({}); // false
isStringObject(["a", "b", "c"]); // false
Checks if the value is a String object (a string wrapped in an object), eg.
new String("abc")orObject("abc"), rather than a primitive string (see isString). A String object has the length, indexes and functions of its string, but it is an object, sotypeofreturns"object", it is truthy even when it is empty, and===(andMap/Setkeys) compare the object rather than its value. Use isStringLike to check for either.Like isDate and isRegExp the check uses objToString, so a String object from another realm (eg. an iframe) is also identified, and on a runtime with
Symbol.toStringTagan object can change the result.The value is narrowed to
String & objectrather thanString, as TypeScript treats a primitivestringas aString, sovalue is Stringwould narrow astring | numbervalue tostring(and tonumberwhen it is false), while a primitive string is never a String object. A value declared asString(which may also be a primitive string) isneverwhen this is false, so declare it asstring | String(which isstringwhen false) or use isStringLike.