In JavaScript, number literals can be represented in various numeral systems using different prefixes. Here are the commonly used prefixes for different numeral systems:
- Decimal: No prefix is required for decimal numbers.
For example:
42
,3.14
,10
. - Binary:
The
0b
or0B
prefix is used to represent binary numbers. Binary numbers consist of only0
and1
. For example:0b1010
represents the binary number1010
, which is equivalent to10
in decimal. - Octal:
The
0o
or0O
prefix is used to represent octal numbers. Octal numbers consist of digits from0
to7
. For example:0o77
represents the octal number77
, which is equivalent to63
in decimal. - Hexadecimal:
The
0x
or0X
prefix is used to represent hexadecimal numbers. Hexadecimal numbers consist of digits from0
to9
and lettersA
toF
(ora
tof
) representing values from10
to15
. For example:0xFF
represents the hexadecimal numberFF
, which is equivalent to255
in decimal.
Here are some examples illustrating the use of different prefixes:
It's important to note that these prefixes are used only for number literals. Once a number is assigned to a variable, the numeral system is not associated with the value itself. Therefore, you can perform arithmetic operations or manipulate the numbers regardless of the numeral system used in the original literal representation.
Additionally, these prefixes are not limited to BigInt
or specific number types. They can be used with regular numbers (number
) as well. However, it's worth mentioning that binary literals and the BigInt
type were introduced in more recent versions of JavaScript, so their support may vary across different environments or older browsers.