What is a Number in TypeScript?
Explore how TypeScript handles numbers, including integers, floats, and various bases like hexadecimal and binary. Understand type inference differences between let and const, how numeric separators improve readability, and the role of NaN in number types. This lesson will help you confidently declare and work with numbers in TypeScript.
We'll cover the following...
Another common primitive is the number. Since TypeScript is a superset of JavaScript, numbers work the same way in both languages. The openness of JavaScript allows for a broad set of numbers. Integers, signed floats, or unsigned floats are permitted. By default, a number will be base 10.
When a type is explicitly assigned to a variable, the type will be removed once the JavaScript is generated. The reason is that typing does not exist in JavaScript. It explains why TypeScript only has number. The following code will produce three variables without an explicit type in JavaScript but if typeof is used, it will return the dynamic type: number.
Number base
You can also assign base 16 (hexadecimal), base 8 (octal) or base 2 ...