Home > Software design >  Number as a JavaScript data type and a global object
Number as a JavaScript data type and a global object

Time:09-17

From this documentation page I read that Number refers to both a data type and a global object. Surely it is not a coincidence that the data type and the global object are both written as Number, right? (I am new to JavaScript with some experience in C and a bit of Python, and it would make more sense to me that Number be both a type and a class.) What is going on here?

I stumbled on this issue by trying to write 1.isInteger(), thinking that 1 is an instance of Number, and I should call the method isInteger on the instance. But then learned that I should write Number.isInteger(1) instead. What exactly is this Number object? Is it an instance of some class?

CodePudding user response:

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.

CodePudding user response:

In JavaScript, number is a primitive - data that is not an object and has no methods or properties.

However, Number is an object which wraps a primitive (number). It is used to represent and manipulate numbers. Its constructor contains constants and methods for working with numbers.

Data type refers both to primitive values and objects. This is why Number is also a data type. Number is a global object because it is an object that always exists in the global scope.

  • Related