I would recommend using JSON.stringify, which converts the set of the variables in the object to a JSON string.

var obj = {
  name: 'myObj'
};

JSON.stringify(obj);

Most modern browsers support this method natively, but for those that don't, you can include a JS version.

Answer from Gary Chambers on Stack Overflow
🌐
W3Schools
w3schools.com › jsref › jsref_obj_string.asp
JavaScript String Reference
HTML wrapper methods return a string wrapped inside an HTML tag. These are not standard methods, and may not work as expected. The HTML wrapper methods are deprecated in JavaScript.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String
String - JavaScript | MDN
The String object is used to represent and manipulate a sequence of characters. Strings are useful for holding data that can be represented in text form.
🌐
TutorialsPoint
tutorialspoint.com › home › javascript › javascript strings object
JavaScript Strings Object
September 1, 2008 - The String object in JavaScript lets you work with a series of characters; it wraps JavaScript's string primitive data type with a number of helper methods.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-strings
JavaScript Strings - GeeksforGeeks
... // Using Single Quote let s1 = 'abcd'; console.log(s1); // Using Double Quote let s2 = "abcd"; console.log(s2); ... The new String() constructor creates a string object instead of a primitive string.
Published   September 25, 2025
🌐
Tutorial Republic
tutorialrepublic.com › javascript-reference › javascript-string-object.php
JavaScript String Properties and Methods - Tutorial Republic
This chapter contains a brief overview of the properties and method of the global String object. The JavaScript String object is a global object that is used to store strings.
🌐
O'Reilly
oreilly.com › library › view › learning-javascript › 0596527462 › ch04s03.html
The String Object - Learning JavaScript [Book]
October 17, 2006 - <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>Exploring String</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <body> <script type="text/javascript"> //<![CDATA[ var sObj = new String( ); var sTxt = sObj.concat("This is a ", "new string"); document.writeln(sTxt); //]]> </script> </body> </html> There is no known limit to the number of strings you can concatenate with the String concat method. However, I rarely use this myself; I prefer the String operators, such as the string concatenation operator (+). The properties and methods available with the String object are listed in Table 4-1.
Author   Shelley Powers
Published   2006
Pages   352
🌐
C# Corner
c-sharpcorner.com › article › array-and-string-object-in-javascript
String Objects in JavaScript
March 17, 2023 - JavaScript String Object is a universal object that is used to store strings.
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Understanding strings as objects - JavaScript - The freeCodeCamp Forum
September 5, 2018 - I’m trying to better understand the internal structure of a string. In the following code, I initialize a string. To see what’s inside, I use Object.getOwnPropertyNames() and Object.values(). var str = 'hi'; console.log(Object.getOwnPropertyNames(str)); console.log(Object.values(str)); ...
Find elsewhere
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › String
String() constructor - JavaScript | MDN
The String() constructor creates String objects. When called as a function, it returns primitive values of type String. ... Note: String() can be called with or without new, but with different effects. See Return value. ... Anything to be converted to a string.
🌐
Techotopia
techotopia.com › index.php › JavaScript_String_Object
JavaScript String Object - Techotopia
It provides a range of methods that can be used to perform a variety of string manipulation tasks (replacing parts of a string with different text, extracting fragments of a string, finding where a particular character appears in a string and much, much more).
Top answer
1 of 12
186

JavaScript has two main type categories, primitives and objects.

var s = 'test';
var ss = new String('test');

The single quote/double quote patterns are identical in terms of functionality. That aside, the behaviour you are trying to name is called auto-boxing. So what actually happens is that a primitive is converted to its wrapper type when a method of the wrapper type is invoked. Put simple:

var s = 'test';

Is a primitive data type. It has no methods, it is nothing more than a pointer to a raw data memory reference, which explains the much faster random access speed.

So what happens when you do s.charAt(i) for instance?

Since s is not an instance of String, JavaScript will auto-box s, which has typeof string to its wrapper type, String, with typeof object or more precisely s.valueOf(s).prototype.toString.call = [object String].

The auto-boxing behaviour casts s back and forth to its wrapper type as needed, but the standard operations are incredibly fast since you are dealing with a simpler data type. However auto-boxing and Object.prototype.valueOf have different effects.

If you want to force the auto-boxing or to cast a primitive to its wrapper type, you can use Object.prototype.valueOf, but the behaviour is different. Based on a wide variety of test scenarios auto-boxing only applies the 'required' methods, without altering the primitive nature of the variable. Which is why you get better speed.

2 of 12
39

This is rather implementation-dependent, but I'll take a shot. I'll exemplify with V8 but I assume other engines use similar approaches.

A string primitive is parsed to a v8::String object. Hence, methods can be invoked directly on it as mentioned by jfriend00.

A String object, in the other hand, is parsed to a v8::StringObject which extends Object and, apart from being a full fledged object, serves as a wrapper for v8::String.

Now it is only logical, a call to new String('').method() has to unbox this v8::StringObject's v8::String before executing the method, hence it is slower.


In many other languages, primitive values do not have methods.

The way MDN puts it seems to be the simplest way to explain how primitives' auto-boxing works (as also mentioned in flav's answer), that is, how JavaScript's primitive-y values can invoke methods.

However, a smart engine will not convert a string primitive-y to String object every time you need to call a method. This is also informatively mentioned in the Annotated ES5 spec. with regard to resolving properties (and "methods"¹) of primitive values:

NOTE The object that may be created in step 1 is not accessible outside of the above method. An implementation might choose to avoid the actual creation of the object. [...]

At very low level, Strings are most often implemented as immutable scalar values. Example wrapper structure:

StringObject > String (> ...) > char[]

The more far you're from the primitive, the longer it will take to get to it. In practice, String primitives are much more frequent than StringObjects, hence it is not a surprise for engines to add methods to the String primitives' corresponding (interpreted) objects' Class instead of converting back and forth between String and StringObject as MDN's explanation suggests.


¹ In JavaScript, "method" is just a naming convention for a property which resolves to a value of type function.

Top answer
1 of 6
27

Speaking about language types, Strings are values of the String type.

The language has five primitive types, which are String, Number, Boolean, Null and Undefined.

There are String objects (also for Number or Boolean), they are called primitive wrappers, they are created when you use the constructor function associated with them, for example:

typeof new String('foo'); // "object"
typeof 'foo';             // "string"

But don't get confused with them, you will rarely need to use primitive wrappers, because even if primitive values are not objects, you can still access their inherited properties, for example, on a string, you can access all members of String.prototype, e.g.:

'foo'.indexOf('o'); // 2

That's because the property accessor (the dot in this case) temporarily converts the primitive value to an object, for being able to resolve the indexOf property up in the prototype chain.

About the constructor function you have in your question, as you know, it won't return the string.

Functions called with the new operator return an implicit value, which is a new object that inherits from that function's prototype, for example:

function Test () {
  // don't return anything (equivalent to returning undefined)
}

new Test() instanceof Test; // true, an object

If an object is returned from the constructor, that newly created object (this within the constructor) will be lost, so the explicit returned object will come out the function:

function Test2() {
  return {foo: 'bar'};
}

new Test2().foo; // 'bar'

But in the case of primitive values, they are just ignored, and the new object from the constructor is implicitly returned (for more details check the [[Construct]] internal operation, (see step 9 and 10)).

2 of 6
3

In JavaScript, strings come in two flavors:

  1. There is a String language type which contains values like "foo" and 'bar'. Those values are primitive values. Read about the String type here

  2. Then there is a String constructor. (A constructor is a function object which is used to create new instances of a certain "class" (or pseudo-class)). So this: new String("foo") will create a new object (a value of the type Object), which contains the primitive value "foo". Read about the String constructor here

In practice you don't use the new String('foo') notation, but the string literal notation 'foo'.


So to answer your question:

In JavaScript, strings are not objects. They are primitive values. However, there exist String objects which can be used to store string values, but those String objects are not used in practice.

🌐
BrainStation®
brainstation.io › learn › javascript › string
JavaScript String (2026 Tutorial & Examples) | BrainStation®
February 4, 2025 - A JavaScript string is a data type for storing textual information. JavaScript strings can be a word or a sentence or an entire block of text.
🌐
Medium
therohantomar.medium.com › understanding-javascript-strings-primitive-vs-object-a65164c72b87
🧠Understanding JavaScript Strings: Primitive vs Object | by Therohantomar | Medium
January 15, 2025 - When you call a method on a primitive string, JavaScript temporarily wraps the string in a String object so that the method can be called. This is done automatically and behind the scenes, so it’s usually not something you need to worry about.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › are-string-objects-in-javascript
Are String Objects in JavaScript? - GeeksforGeeks
July 23, 2025 - The confusion arises because strings appear to have methods and properties like objects, but they are primitives. The temporary wrapping of primitives into String objects is what enables this behavior. ... Here, JavaScript implicitly wraps the string in a String object to access the .constructor property.
🌐
W3Schools
w3schools.com › js › js_string_methods.asp
JavaScript String Methods
JS Examples JS HTML DOM JS HTML Input JS HTML Objects JS HTML Events JS Browser JS Editor JS Exercises JS Quiz JS Website JS Syllabus JS Study Plan JS Interview Prep JS Bootcamp JS Certificate JS Reference ... Javascript strings are primitive and immutable: All string methods produce a new string without altering the original string.
🌐
Acid & Base
sceweb.sce.uhcl.edu › helm › WEBPAGE-Javascript › my_files › Object › Module-4 › module4page.html
Strings Object
The String object lets you work with a series of characters; it wraps Javascript's string primitive data type with a number of helper methods.