Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between a number and a number object?

Tags:

javascript

What is the difference between a number stored in a normal variable:

var foo = 5; 

And a number object:

var bar = new Number(5);

What can I use a number object for?

like image 549
Muhammad Avatar asked Sep 15 '13 14:09

Muhammad


People also ask

What Is a Number object?

The Number object represents numerical date, either integers or floating-point numbers. In general, you do not need to worry about Number objects because the browser automatically converts number literals to instances of the number class.

Whats the difference between number and number TypeScript?

If you use Number in a place where a type is expected, TypeScript will not complain, because Number is an interface. If you use Number in a place where a value (something that exists in emitted code) is expected, TypeScript will not complain, because Number is also a global constructor.

What is the use of a number object in JavaScript?

The JavaScript number object enables you to represent a numeric value. It may be integer or floating-point. JavaScript number object follows IEEE standard to represent the floating-point numbers. By the help of Number() constructor, you can create number object in JavaScript.

Can an object property be a number?

Can JavaScript object property be a number? Against what many think, JavaScript object keys cannot be Number, Boolean, Null, or Undefined type values. Object keys can only be strings, and even though a developer can use other data types to set an object key, JavaScript automatically converts keys to a string a value.


2 Answers

A Number object contains some useful methods and properties such as:

Number Object Methods

Method                       Description
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
toExponential(x)    Converts a number into an exponential notation
toFixed(x)          Formats a number with x numbers of digits after the decimal point
toPrecision(x)      Formats a number to x length
toString()          Converts a Number object to a string
valueOf()           Returns the primitive value of a Number object

Number Object Properties

Property                        Description
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
constructor         Returns the function that created the Number object's prototype
MAX_VALUE       Returns the largest number possible in JavaScript
MIN_VALUE           Returns the smallest number possible in JavaScript
NEGATIVE_INFINITY   Represents negative infinity (returned on overflow)
NaN             Represents a "Not-a-Number" value
POSITIVE_INFINITY   Represents infinity (returned on overflow)
prototype           Allows you to add properties and methods to an object
like image 58
Charaf JRA Avatar answered Nov 15 '22 12:11

Charaf JRA


I think in practice there is no difference between this two. All available methods for number objects is also available for primitive numbers. When you invoke a method on a primitive number, the number temporarily gets converted to an object and then the method executes. See the following examples:

var temp1 = Object(1) // number object   
var temp2 = 1         // primitive number

console.log(temp1.toString()) // invoke its own method. result: 1
console.log(temp2.toString()) // temporarily converts to object and invoke number object method. result:1

console.log(Object(1).constructor === Number) //true
console.log((1).constructor === Number)       //true
//             ^---------- temporarily converts to object
like image 25
frogatto Avatar answered Nov 15 '22 14:11

frogatto