2
var a = 10
var b = new Number(10)

console.log(a instanceof Number)
console.log(b instanceof Number)

can anyone please let me know what is the difference between above two declaration and definition of a and b.

Nisarg Shah
  • 14,151
  • 6
  • 34
  • 55
Piyush Biswal
  • 137
  • 1
  • 8
  • 1
    Maybe this post can help explain [**new Number()**](https://stackoverflow.com/questions/2381399/what-is-the-difference-between-new-number-and-number-in-javascript) The rest I'm sure will make sense after you understand `new Number()` – NewToJS Oct 17 '17 at 03:05
  • 1
    Possible duplicate of [What is the difference between string literals and String objects in JavaScript?](https://stackoverflow.com/questions/17256182/what-is-the-difference-between-string-literals-and-string-objects-in-javascript) – Chang Oct 17 '17 at 03:09
  • 1
    poorly worded title, both `a` and `b` are `var` – Jaromanda X Oct 17 '17 at 03:10

4 Answers4

1

Explicitly stating that you are creating a new number with new Number(10) creates a new wrapper object for the number, whereas simply defining a number as a variable creates a integer primitive value. As such, you get differing results when checking their typeof:

var a = 10;
var b = new Number(10);

console.log(typeof a);
console.log(typeof b);

Hope this helps! :)

Obsidian Age
  • 41,205
  • 10
  • 48
  • 71
  • Pretty sure it creates an number primitive value... the rest seems right though. – Shadow Oct 17 '17 at 03:10
  • 1
    Whoops, slight oversight on my part :P Fixed :) – Obsidian Age Oct 17 '17 at 03:11
  • thanks buddy for the clarification but when we declare var a=10, we find a number of methods against a variable but without a constructor . so can you explain this one too as , there is no integer concept in javascript and JavaScript numbers are always stored as double precision floating point numbers. – Piyush Biswal Oct 17 '17 at 05:55
1

The first creates a primitive. The other an object.

The primary uses for the Number object are:

1) If the argument cannot be converted into a number, it returns NaN.

2) In a non-constructor context (i.e., without the new operator), Number can be used to perform a type conversion.

reference

In theory there is a difference but in practice none. The JavaScript engine automagicly boxes a primitive to an object when it needs to be an object.

var number = 42;
// calling .toFixed will 'box' the primitive into a number object,
// run the method and then 'unbox' it back to a primitive
console.log( number.toFixed(2) );

The only usage I've found is if you want to return a primitive from a constructor function.

function Foo() {
    return 42;
}

var foo = new Foo();
console.log(foo); // foo is instance of Foo

function Bar() {
    return new Number(42);
}

var bar = new Bar();
console.log(bar); // bar is instance of Number

remind that

new Number(10) == new Number(10) return false

enter image description here

Masum billah
  • 982
  • 7
  • 24
0

The first declaration is like primitive datatype.

While in your second declaration where you used new Number(10), it is considered like a wrapper function or class to preserve your number in an object.

To see the actual difference, check now the actual type of your variables in both cases:

console.log(typeof(a)) //should give you "number"
console.log(typeof(b)) //should give you "object"
Muhammad Soliman
  • 21,644
  • 6
  • 109
  • 75
0

The var keyword is just a simple declaration of a variable. The new keyword is used to generate a new object of some type. You'll need to look into objects in programming in general in order to fully understand them.

B. Fleming
  • 7,170
  • 1
  • 18
  • 36