2

From JavaScript: The Definitive Guide by David Flanagan

JavaScript types can be divided into two categories: primitive types and object types. JavaScript’s primitive types include numbers, strings of text (known as strings), and Boolean truth values (known as booleans).

It states that the string is primitive type. But later on there was an example code

var s = "hello, world" // Start with some text.
s.charAt(0) // => "h": the first character.
s.charAt(s.length-1) // => "d": the last character.
s.substring(1,4) // => "ell": the 2nd, 3rd and 4th characters.
s.slice(1,4) // => "ell": same thing
s.slice(-3) // => "rld": last 3 characters
s.indexOf("l") // => 2: position of first letter l.
s.lastIndexOf("l") // => 10: position of last letter l.
s.indexOf("l", 3) // => 3: position of first "l" at or after 3

So is string object type or primitive? how can a primitive type have methods? Isn't this object type property? If it's some kind of hybrid of both, then when it is primitive and when it is object?

lychee
  • 1,771
  • 3
  • 21
  • 31
  • 3
    Keep reading, you'll find the explanation on your own. Also this is not a question you should ask on SO, since you have no problem with your code, just a lack of understanding of the language. – Touffy Mar 28 '15 at 18:27
  • 1
    Suggested reading: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String – nicael Mar 28 '15 at 18:27
  • Primitve types can also have got methods like object types. But you can not assign new methods/properties to literal primitive types but you can assign new methods/properties to literal object types. That's one difference between primitves and objects. – Blauharley Mar 28 '15 at 18:28
  • From @nicael's link, you can even skip directly to [Distinction between string primitives and String objects](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String#Distinction_between_string_primitives_and_String_objects). – Evan Davis Mar 28 '15 at 18:31

1 Answers1

2

Strings are primitive values:

4.3.2 primitive value

member of one of the types Undefined, Null, Boolean, Number, or String

But there are also string objects, which are objects and not primitives:

4.3.18 String object

member of the Object type that is an instance of the standard built-in String constructor

It may seem primitive strings have properties, but no.

When you use for example string.charAt(0), a string object is created with the same value as your primitive string. This object inherits from String.prototype. The charAt method of this string object is called, and the returned value is returned in string.charAt(0). Then the string object is removed.

When you assign a property to a primitive string, something similar happens: the property is assigned to the new string object instead of to the primitive.

Some examples:

var primitiveStr = 'foo',
    objectStr = new String('foo');
typeof primitiveStr; // 'string'
typeof objectStr; // 'object'
typeof primitiveStr.charAt; // 'function'
typeof objectStr.charAt; // 'function'
primitiveStr.foo = objectStr.foo = 123;
typeof primitiveStr.foo; // 'undefined'
typeof objectStr.foo; // 'number'
Oriol
  • 274,082
  • 63
  • 437
  • 513