JavaScript String Objects: What They Are and How They Work

How-to basics · Updated November 22, 2026

A JavaScript string object is an object wrapper around a string value, usually created with new String(value). In everyday code, use a string primitive such as "hello" instead: JavaScript automatically lets primitives use string methods, while wrapper objects can cause confusing type and comparison behavior.

What “String object” means in JavaScript

JavaScript has two related but distinct things that people may call a string. A string primitive is a basic value containing text, such as "hello". A String object is an object whose internal string data contains text. The built-in String function and String.prototype provide conversion behavior and methods such as toUpperCase(), includes(), and slice().

This distinction explains why these expressions produce different types:

  • typeof "hello" evaluates to "string".
  • typeof new String("hello") evaluates to "object".
  • String("hello") returns the primitive string "hello".

The object wrapper exists for language behavior and compatibility, but it is rarely needed in ordinary application code. A primitive can still access methods and properties because JavaScript temporarily wraps it when necessary. That automatic behavior is why "hello".toUpperCase() works without explicitly constructing a String object.

Creating strings: primitive conversion versus construction

Calling String(value) as a function converts a value to a primitive string. Calling it with new constructs a wrapper object. The difference is important:

const primitive = String(42);
const wrapper = new String(42);

console.log(primitive);        // "42"
console.log(typeof primitive); // "string"
console.log(typeof wrapper);   // "object"

For example, String(null) produces the text "null", and String(true) produces "true". These are conversions, not special string literals. Calling String() with no argument returns an empty string. By contrast, new String() creates a wrapper around an empty string.

In most code, use a string literal or a template literal when you already know the text, and use String(value) when you intentionally need conversion. Avoid new String(value) unless an API specifically requires an object wrapper, which is uncommon. The new form does not make text more permanent, safer, or more capable.

Why string methods work on primitive values

Primitives are not objects, but JavaScript permits many object-style operations on them. When code reads a method such as toLowerCase from a string primitive, the language performs a temporary object conversion so it can find the method on String.prototype. The method runs and returns a result, usually another primitive string. The temporary wrapper is not retained as the variable’s value.

For instance, const name = "Mira"; name.toLowerCase() returns "mira", while name remains "Mira". Common string operations include:

  • includes(searchString) to check whether text occurs within a string.
  • startsWith(prefix) and endsWith(suffix) to check boundaries.
  • slice(start, end) to extract part of a string.
  • replace(searchValue, replacement) to create text with a replacement.
  • trim() to return a version without leading and trailing whitespace.

These methods do not modify the original string. They return values or information; methods that appear to change text create a new string. For example, replace() returns changed text but does not update the variable unless the result is assigned.

String values are immutable

Both string primitives and the text data inside String objects are immutable. Once a string value exists, its characters cannot be edited in place. An expression like text[0] = "H" does not change the first character. To produce different text, create a new string, for example with concatenation, a template literal, or a method such as replace().

A wrapper object itself can have ordinary object properties, which is separate from changing its string data. For example, a program may attach a custom property to a String object. That property does not alter the wrapped text, and it does not turn the wrapper into a useful mutable character collection. Primitive strings cannot retain custom properties: in strict mode, attempting to assign one generally throws a TypeError; outside strict mode, the assignment is ignored.

For repeated text changes, it is often clearer to build a new result or collect pieces in an array and join them. JavaScript engines can optimize string operations, so performance should be measured in the actual application rather than assumed from the presence or absence of a wrapper.

Comparing primitives and String objects

A frequent source of bugs is comparing a wrapper object with a primitive. Strict equality checks both type and value for primitives, but for objects it checks identity—whether two references point to the same object. Consequently, new String("cat") === "cat" is false: one operand is an object and the other is a primitive. Two separately created wrappers are also unequal under strict equality, even if they contain identical text.

const a = new String("cat");
const b = new String("cat");

console.log(a === "cat"); // false
console.log(a === b);     // false
console.log(a == "cat"); // true

The last comparison uses loose equality, which coerces values in this case. Although it may appear convenient, relying on implicit coercion can make comparisons harder to reason about. Prefer primitive strings and strict equality. If a value might be a wrapper and you need its text, explicitly convert it with String(value) when that conversion is appropriate.

Similarly, an object used as a key in a Map is identified by object identity, not by the text it wraps. Two String objects containing the same characters are distinct keys. A primitive string key is usually the predictable choice for text-based lookups.

Length, indexing, and Unicode details

JavaScript strings are sequences of UTF-16 code units. The length property counts those code units, not necessarily the number of visible characters. Basic characters such as the letter A use one code unit, while many emoji and some other symbols use two. Thus, "😀".length is 2 even though the displayed symbol is usually perceived as one character.

Indexing a string, as in text[0], accesses a UTF-16 code unit position. A result may therefore be only half of a character represented by a surrogate pair. When iterating by Unicode code point, for...of or Array.from(text) handles surrogate pairs as one iteration item. Even code points are not always the same as user-perceived characters: a visible symbol may be made from multiple code points, such as a base letter combined with a diacritic or a joined emoji sequence.

For interfaces that need to count or move through user-perceived characters, use grapheme-cluster-aware processing, such as Intl.Segmenter where supported, rather than assuming that length equals the number of displayed characters. Also note that visually identical text can have different Unicode encodings. JavaScript does not automatically normalize strings; String.prototype.normalize() can be used when canonical Unicode normalization is needed.

Practical choices and common mistakes

For most JavaScript variables, function arguments, object properties, and API data, use primitive strings. This keeps values straightforward to compare, serialize, and inspect. A practical rule is to distinguish conversion from construction:

  • Use "status" or `status` to write a string literal.
  • Use String(value) when you deliberately want a primitive string representation.
  • Avoid new String(value) for normal application data.
  • Check input types when an API requires a string rather than silently accepting arbitrary values.

Conversion does not validate that a value is suitable for every purpose. For example, converting an object to a string may produce a generic representation rather than meaningful user-facing text. Likewise, converting untrusted input does not make it safe for HTML, SQL, a shell command, or a URL. Use the appropriate context-specific validation or escaping method when inserting text into those contexts.

Another misconception is that a string object is needed to call string methods. It is not: primitives have access to the same standard methods through automatic wrapping. A second misconception is that length always counts letters or symbols as people see them; it counts UTF-16 code units. Knowing these distinctions helps avoid type surprises and text-processing errors.

Frequently Asked Questions

Is a JavaScript string a primitive or an object?
A quoted string such as "hello" is a primitive. new String("hello") creates an object wrapper. Both represent text, but their types and equality behavior differ.

Should I ever use new String()?
Usually not. Use a primitive string for text and call String(value) for conversion. Constructing a wrapper adds object behavior that can create unexpected comparisons and type checks without providing a normal string-processing advantage.

Why does typeof a string sometimes return object?
It returns "object" when the value is a String wrapper created with new String(). A primitive string returns "string". Check whether the code used the constructor with new or received a wrapper from another source.

Are JavaScript strings mutable?
No. String primitives cannot be edited in place, and a String object’s wrapped text is also immutable. Operations such as replace() return a new string; assign that result if you want to keep it.

Why is an emoji sometimes length 2?
The JavaScript length property counts UTF-16 code units. Many emoji are encoded using a surrogate pair, which occupies two code units. Some displayed emoji sequences contain multiple code points, so counting visible characters may require grapheme-aware segmentation.

Related

Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.

More in How-to basics

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.