1

I'm not a JavaScript newbie, but I was reading MDN page on primitive data types and it caught my attention the following line:

A primitive (primitive value, primitive data type) is data that is not an object and has no methods. In JavaScript, there are 6 primitive data types: string, number, boolean, null, undefined, symbol (new in ECMAScript 2015).

Source: https://developer.mozilla.org/en-US/docs/Glossary/Primitive

If primitives have no methods, then why is it possible to do this in JavaScript:

'HELLO WORLD'.toLowerCase() // result: hello world

Why isn't that invalid? Shouldn't that throw an "Uncaught SyntaxError: Invalid or unexpected token" just as the following statement does:

2.toString()

ILikeTacos
  • 17,464
  • 20
  • 58
  • 88
  • 1
    `length` is a property not a method – klugjo Dec 10 '17 at 01:03
  • @klugjo irrelevant in this context, but edited for clarity. – ILikeTacos Dec 10 '17 at 01:04
  • might want to read this: https://javascriptweblog.wordpress.com/2010/09/27/the-secret-life-of-javascript-primitives/ – display name Dec 10 '17 at 01:06
  • true.toString() does not give an error – Musa Dec 10 '17 at 01:06
  • seems misleading since most have prototype methods – charlietfl Dec 10 '17 at 01:07
  • 1
    The reason `2.toString()` fails is compilers always look at dot after number as decimal and expect number to follow. Can do `(2).toString()` – charlietfl Dec 10 '17 at 01:08
  • 1
    `2.toString()` gives an error because the `.` is considered a decimal point. `2..toString()` maybe https://stackoverflow.com/questions/12272581/why-doesnt-javascript-let-you-call-methods-on-numbers-directly – Musa Dec 10 '17 at 01:09
  • 3
    I believe you have discovered a mistake in MDN, well done! The ECMA specification's [definition of "primitive"](https://www.ecma-international.org/ecma-262/6.0/#sec-primitive-value) says nothing about whether one should or should not have methods. – qntm Dec 10 '17 at 01:10

0 Answers0