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.
Answer from Omer Sherer on Stack OverflowNumber as a JavaScript data type and a global object - Stack Overflow
javascript - What is the difference between "number" and "Number" in TypeScript? - Stack Overflow
Why do I got "Type 'string | number' is not assignable to type 'never'. Type 'string' is not assignable to type 'never'. " it's make me misleading
Old meme format, timeless JavaScript quirks
Videos
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.
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.
number is only a TypeScript thing - it's a primitive type referring to, well, a number.
But, judging by the error message, it seems that number isn't actually a discrete value like Number.
Indeed - it's a type, so it doesn't exist in emitted code.
a discrete value like Number. On the other hand, user-defined types like classes appear to be discrete values (as they also print the standard function description).
Yes. Classes, of which Number is one, are special. They do two things, somewhat unintuitively:
- They create a JavaScript class (usable in emitted code)
- They also create an interface for the class (only used by 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.
In other words, the two Numbers below refer to completely different things:
// refer to the TypeScript Number interface
let foo: Number;
// refer to the JavaScript global.Number constructor
const someNum = Number(someString);
Using Number in TypeScript is very odd, since it'd, strictly, speaking, refer to a number created via new:
const theNum = new Number(6);
Which there's almost never a reason to do. Use a plain primitive number instead, without an object wrapper.
const theNum = 6;
// theNum is typed as `number`
From https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#the-primitives-string-number-and-boolean:
JavaScript has three very commonly used primitives:
string,number, andboolean. Each has a corresponding type in TypeScript. As you might expect, these are the same names you’d see if you used the JavaScript typeof operator on a value of those types:
stringrepresents string values like"Hello, world"numberis for numbers like42. JavaScript does not have a special runtime value for integers, so there’s no equivalent tointorfloat- everything is simplynumberbooleanis for the two valuestrueandfalseThe type names
String,Number, andBoolean(starting with capital letters) are legal, but refer to some special built-in types that will very rarely appear in your code. Always usestring,number, orbooleanfor types.
(There are also typs for null and undefined)