13

MDN states:

primitive, primitive value

A data that is not an object and does not have any methods. JavaScript has 5 primitive datatypes: string, number, boolean, null, undefined. With the exception of null and undefined, all primitives values have object equivalents which wrap around the primitive values, e.g. a String object wraps around a string primitive. All primitives are immutable.

So when we call a "s".replace or "s".anything is it equivalent to new String("s").replace and new String("s").anything?

Community
  • 1
  • 1
Pacerier
  • 86,231
  • 106
  • 366
  • 634

2 Answers2

17

No, string primitives do not have methods. As with numeric primitives, the JavaScript runtime will promote them to full-blown "String" objects when called upon to do so by constructs like:

var space = "hello there".indexOf(" ");

In some languages (well, Java in particular, but I think the term is in common use) it's said that the language "boxes" the primitives in their object wrappers when appropriate. With numbers it's a little more complicated due to the vagaries of the token grammar; you can't just say

var foo = 27.toLocaleString();

because the "." won't be interpreted the way you'd need it to be; however:

var foo = (27).toLocaleString();

works fine. With string primitives — and booleans, for that matter — the grammar isn't ambiguous, so for example:

var foo = true.toString();

will work.

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • heys btw is the double dot syntax "standard" ? Like 27..toLocaleString() – Pacerier Apr 22 '11 at 02:46
  • Well it seems to work in Chrome at least ... the issue is that the low-level "token grammar" will hook the first "." onto the number, because of the way floating-point numbers look ("23.223"). But that double-dot trick *should* work, because the tokenizer will stop at the second ".", leaving it to the parser to construct the property reference expression. – Pointy Apr 22 '11 at 02:52
  • Personally, I'd prefer `(27).toLocaleString()` because it's easy to lose track of a little "." stuck in there. But that's just a stylistic opinion and it isn't worth much :-) – Pointy Apr 22 '11 at 02:54
  • 1
    The first dot after a digit is a decimal point. All browsers have always agreed that 12..toLocaleString() is operating on the number. – kennebec Apr 22 '11 at 03:31
  • @kennebec that makes good sense, thanks; I haven't personally been keeping track over the past dozen years, but it certainly makes sense given the token grammar etc. :-) – Pointy Apr 22 '11 at 12:03
  • Is this "auto-promotion" of primitive values to wrapped objects explicitly stated in the spec? – wlnirvana Apr 20 '21 at 08:28
  • I think the answer to my previous comment is "yes". See [this post](https://stackoverflow.com/a/8582032/2160440) – wlnirvana Apr 20 '21 at 08:58
8

The technically correct answer is "no".

The real-world answer is "no, but it will work anyway". That's because when you do something like

"s".replace()

the interpreter knows that you want to actually operate on the string as if you had created it with

var str = new String("s")

and therefore acts as if you had done that.

helloandre
  • 10,541
  • 8
  • 47
  • 64