4

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.

2
  • 1
    A number of built-in types like String, Date, Array and also objects that have methods or may be automatically converted to objects as needed. Why is this hard to believe or understand? Commented Aug 10, 2013 at 22:47
  • Go to your browser's console and type typeof new String("s") and also typeof "asfd" Commented Aug 10, 2013 at 22:47

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

2 Comments

Sorry,I'm new to SO,cant upvote the answer.(min 15 reputation).I have accepted your answer.
@vetexion No problem. There's an outline of a check mark near the upvote button and if you click it, it should turn green. meta.stackexchange.com/questions/91889/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.