Number
refers to a global constructor function that, when called, returns a number (or, in very strange cases, a Number object).
number
is the string returned by the typeof
operation - or, the word one uses to describe a number primitive.
It's only a capitalization difference, but it's quite an important distinction.
const str = '34';
const num = Number(str);
console.log(typeof num);
What exactly is this Number object? Is it an instance of some class?
Number
itself is a class, kind of - a function that can be called, possibly with new
, which will return a value that inherits from Number.prototype
. (Although it's not syntactically forbidden, best to never use new
with Number
)
Unlike some languages, JavaScript types aren't really values you can work with and manipulate inside a script. You can say, when looking at a piece of code: "This variable points to a number" - and you can use the typeof
operator to extract the string representation of the type from a value - but that's it. In actual JavaScript code, there isn't a number
value or type that can be referenced in the code to do something with (other than when as part of a string in conjunction with the use of typeof
). This is in contrast with, for example, TypeScript, where you could do type NumObj = { theValue: number }
and declare const theMap: Map<number, string>
, and lots of other interesting things - but JavaScript only has the 'number'
that typeof
returns, and the Number constructor and its methods.
Number
is a constructor function, but it also has static methods - methods directly on the constructor, not on the prototype object, such as isInteger
- a utility function related to numbers that isn't that particular to a given number instance.