Question

I have recently seen some references that explain Strings in Javascript as a primitive type.I know that a primitive is a data type that is composed of no other data types and can not be broken down any further.But the problem is I have also read strings are objects.How it can be both ?Please clarify me about the confusion.

Was it helpful?

Solution

You can read about that exact topic on MDN:

Note that JavaScript distinguishes between String objects and primitive string values. (The same is true of booleans and numbers.)

String literals (denoted by double or single quotes) and strings returned from String calls in a non-constructor context (i.e., without using the new keyword) are primitive strings. JavaScript automatically converts primitives to String objects, so that it's possible to use String object methods for primitive strings. In contexts where a method is to be invoked on a primitive string or a property lookup occurs, JavaScript will automatically wrap the string primitive and call the method or perform the property lookup.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top