DEV Community

Discussion on: JavaScript Data Types

Collapse
 
ephraimdavid22 profile image
Agbeze_Obinna • Edited

If a string is an example of primitive type which doesn't have a prop or method, then why this...??

//string methods

'obinna ephraim'.toLowerCase() 
'Nora dimma'.trim()
Collapse
 
juliansci profile image
Julián Scialabba

Hi!
I have explained it in the String section. It is because Javascript internally creates an object wrapper for each primitive type (except null and undefined).
For this reason the primitive values are treated as objects when executing methods and properties.
In your example, Javascript internally does this:

String('obinna ephraim').toLowerCase();
String('Nora dimma').trim();
Collapse
 
pentacular profile image
pentacular

I think it's important to say that it 'effectively' does this.

It's unlikely that an implementation will actually do that, and probable that there will be some efficient dispatch table somewhere that does the right thing without going around creating lots of ephemeral String objects. :)