18

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?

Marcel Gwerder
  • 8,353
  • 5
  • 35
  • 60
Muhammad
  • 193
  • 1
  • 7

3 Answers3

14

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
Marcel Gwerder
  • 8,353
  • 5
  • 35
  • 60
Charaf JRA
  • 8,249
  • 1
  • 34
  • 44
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number MDN Documentation on Number object –  Sep 15 '13 at 14:28
  • But how does this answer relate to the `new Number(123)` that the OP posted in a comment above? (I know that comment was posted after this answer so it's certainly not your fault FaceOfJock, but now that the question has been clarified this doesn't really answer it.) – nnnnnn Sep 15 '13 at 14:36
  • the question have been edited by a moderator ,it's not the op question – Charaf JRA Sep 15 '13 at 14:38
  • @FaceOfJock The comment came from the OP but it's true that I've edited the question because it was very unclear. – Marcel Gwerder Sep 15 '13 at 14:41
  • @MarcelGwerder until now , i think that this is the difference , he needs just to replace x parameter by 123 in the example and test – Charaf JRA Sep 15 '13 at 14:43
  • In addition to the methods mentioned here, you can attached metadata to the number. Such as: `foo.first = false`. If you attempt to attach this to a plain number the attachment will be silently dropped. – eadsjr Oct 15 '19 at 05:31
8

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
frogatto
  • 28,539
  • 11
  • 83
  • 129
  • 10
    "*I think in practice there is no difference between this two*" `foo = new Number(5); bar = new Number(5); console.log(foo === bar)` behaves COMPLETELY DIFFERENTLY to `foo = 5; bar = 5; console.log(foo === bar)` because two different *objects* are not equal, while two number primitives with the same value - are. – VLAZ Jul 05 '19 at 12:34
2
var foo = 5; 
typeof(foo); // Is Number

var bar = new Number(5);
typeof(bar); // Is object

When you go down to advanced level in JavaScript, you have certain properties for objects which you can't invoke on numbers, so it is up to you to draw a line and see what to use where.

Numbers – e.g. 5, 3e+10 (all numbers behave as floats – significant for division, but can be truncated by x >>> 0). Sometimes boxed. Instance of Number when boxed.

Objects – e.g. {foo: 'bar', bif: [1, 2]}, which are really just hashtables. Always boxed. Instance of Object.

defau1t
  • 10,593
  • 2
  • 35
  • 47
  • _" advanced level in JavaScript, you have certain properties for objects which you can't invoke on numbers"_ - Could you please give an example or two? – nnnnnn Sep 15 '13 at 14:26