Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are all variables in js converted to objects before being executed?

Tags:

javascript

From what I have understood, primitive data types such as a string can also have properties ie string can have .includes . Does that mean all primitive data types are converted to objects before being executed. If yes, what is the point of having distinct difference between primitive data types and objects.

like image 308
Zubair Mahmood Avatar asked Nov 21 '25 15:11

Zubair Mahmood


1 Answers

Whenever you try to refer to a property of a string s, JavaScript converts the string value to an object as if by calling new String(s). This object inherits string methods and is used to resolve the property reference. Once the property has been resolved, the newly created object is discarded. Numbers and booleans have methods for the same reason that strings do: a temporary object is created using the Number() or Boolean() constructor, and the method is resolved using that temporary object.

source Javascript: The Definitive Guide. David Flanagan

like image 77
max Avatar answered Nov 24 '25 05:11

max