![]() |
VOOZH | about |
Strings in JavaScript are one of the primitive data types, alongside numbers, booleans, null, undefined, bigint, and symbols. Unlike objects, primitives do not have methods or properties of their own. However, when you attempt to call a method or access a property on a string, JavaScript automatically wraps the string in a String object to provide access to methods.
string 5 42 true
JavaScript has a built-in String object that can be explicitly used to create string objects. This is different from primitive strings.
object
| Feature | Primitive String | String Object |
|---|---|---|
| Type | "string" | "object" |
| Memory Efficiency | More efficient | Less efficient |
| Use Case | Highly used in creating string | Rarely used |
Tip: Always prefer primitive strings for simplicity and performance.
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.
true
Here, JavaScript implicitly wraps the string in a String object to access the .constructor property.