Search code examples
javascriptstringjavascript-objectsprimitive

Javascript primitives


Im still new to JS, learning about objects at the moment and a little confused on primitives. on W3schools a javascript primitive is defined as:

"A primitive value is a value that has no properties or methods.

A primitive data type is data that has a primitive value.

JavaScript defines 5 types of primitive data types:

string

number

boolean

null

undefined"

per: W3Schools

but isnt a string an object and has methods such as string.prototype.indexOf() and string.prototype.toUpperCase() those are considered methods right? What am i missing?


Solution

  • it is actually the difference between string and String. string is primitive but String is object.

    var str = "string"  //primitive
    var str1 = new String("string") //object
    

    when you apply a method to str of String object class, it is automatically converted to the object.

    Auto-boxing is the process whereby the JS will convert primitive data types to their corresponding object wrapper classes. For example, string will be converted to String