0% found this document useful (0 votes)
3 views2 pages

String Primitives vs. String Objects in JS

String primitives in JavaScript are immutable values representing sequences of characters, created using quotes, while String objects are created using the new String() constructor and behave like objects. The key differences include type, creation method, performance, and equality comparisons, with primitives being faster and recommended for most situations. String objects are rarely needed and can lead to subtle bugs due to their object nature.

Uploaded by

ignou07168
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

String Primitives vs. String Objects in JS

String primitives in JavaScript are immutable values representing sequences of characters, created using quotes, while String objects are created using the new String() constructor and behave like objects. The key differences include type, creation method, performance, and equality comparisons, with primitives being faster and recommended for most situations. String objects are rarely needed and can lead to subtle bugs due to their object nature.

Uploaded by

ignou07168
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Difference Between String Objects and String Primitives in JavaScript

String Primitives (string):


A string primitive is a basic, immutable value representing a sequence of characters. It is
created by enclosing text in single ('...'), double ("..."), or backtick (`...`) quotes.
Example:
let str1 = "hello";
let str2 = 'world';
let str3 = `template`;

String primitives are not objects; they are primitive data types [1] [2] [3] .
When you use methods or properties on a string primitive (like [Link] or
[Link]()), JavaScript temporarily wraps the primitive in a String object so you
can use object methods, then discards the object [4] [5] .
The typeof operator returns "string" for string primitives [3] .
String Objects (String):
A String object is created using the new String() constructor.

Example:
let strObj = new String("hello");

String objects are of type "object" and are instances of the built-in String wrapper class [2]
[3] .

They behave like objects, not primitives, which can lead to subtle bugs (for example, strObj
=== "hello" is false because one is an object and the other is a primitive) [3] .

String objects are rarely needed in practice and can negatively affect performance and
code clarity [6] [3] .

Key Differences
Feature String Primitive (string) String Object (String)

Type Primitive Object (wrapper)

Creation 'text', "text", `text` new String('text')

typeof
"string" "object"
result
Feature String Primitive (string) String Object (String)

str1 === str2 is true if values strObj === str2 is false even if contents
Comparison
match match

Performance Faster, more efficient Slower, uses more memory

Use case Recommended for almost all situations Rarely needed

Practical Example

let primitive = "hello";


let object = new String("hello");

[Link](typeof primitive); // "string"


[Link](typeof object); // "object"
[Link](primitive === "hello"); // true
[Link](object === "hello"); // false
[Link](object == "hello"); // true (value equality)

Additional Notes
JavaScript automatically converts string primitives to String objects when you access
methods or properties, so you rarely need to create String objects manually [4] [5] .
String objects and string primitives share most behaviors, but there are important
differences in type checks, comparisons, and performance [3] .
Always prefer string primitives unless you have a specific reason to use the object form [2]
[6] [3] .

"You should rarely find yourself using String as a constructor." - MDN [3]

1. [Link]
2. [Link]
and-string-literal-in-javascript-
3. [Link]
4. [Link]
tring-objects-in-javascrip
5. [Link]
6. [Link]

You might also like