Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the purpose of numeric/boolean/string objects as opposed to primitive values?

Tags:

javascript

In javascript you can call a function as a function or as a constructor. For example you can do :

myObject = new Number(13);
myPrimitiveValue = Number(13);

or simply

myPrimitiveValue = 13;

I understand the difference between the results. Can you explain me under which reasonable circumstances creating a number, a boolean or a string as an object is desirable? For example, ability to set new properties (this is something you can do on objects but can't really do on primitive values) is almost always a bad idea for objects containing number/boolean/string. Why would I want a numeric/boolean/string object?

like image 787
Andrew Savinykh Avatar asked Dec 08 '25 09:12

Andrew Savinykh


1 Answers

You rarely have to create objects for the basic data types.

From the Microsoft documentation of the Number object:

"The Number object is a wrapper for numeric data. The primary purposes for the Number object are to collect its properties into one object and to allow numbers to be converted into strings via the toString method. The Number object is similar to the Number data type. However, they have different properties and methods.

You rarely need to construct a Number object explicitly. The Number data type should be used in most circumstances. Since the Number object interoperates with the Number data type, all Number object methods and properties are available to a variable of type Number."

like image 118
Guffa Avatar answered Dec 09 '25 23:12

Guffa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!