Working with Numbers Flashcards
What is a value?
Values are chunks of bits that represent pieces o information.
In JavaScript those values can be clasified by its primitive type number
, string
, boolean
, undefined
, null
, Symbol
and BigInt
.
number values
Values of number
type are, unsurprinsinghly, numeric values. In Javascript they are written as follows:
- Whole numers (aka integers) are written with digits :
13
- Fractional numers are written by using a dot:
19.81
- For very big or very small numbers, you may also use scientific notation by adding an
e
(for exponent), followed by the exponent of a number.2.99e8
is equivalent to2.99 * 10^8
Is it possible to overflow a number
in JavaScript?
In JavaScript, numbers are represented using the IEEE 754 floating-point format, specifically the double-precision 64-bit binary format. This means that numbers have a finite range and precision. When a number exceeds the maximum representable value, it is set to Infinity or Number.POSITIVE_INFINITY
. Similarly, when a number is below the minimum representable positive value, it underflows to Number.NEGATIVE_INFINITY
.
“Number - JavaScript | MDN” (developer.mozilla.org). Retrieved June 19, 2023.
What is the maximum numeric value representable in JavaScript?
Number.MAX_VALUE
or approximately 1.8E+308
“Number - JavaScript | MDN” (developer.mozilla.org). Retrieved June 19, 2023.
What is the minimum numeric value representable in JavaScript?
Number.MIN_VALUE
or 5E-324
“Number - JavaScript | MDN” (developer.mozilla.org). Retrieved June 19, 2023.
What is Number.EPSILON
?
The Number.EPSILON
static data property represents the difference between 1 and the smallest floating point number greater than 1.
“Number.EPSILON - JavaScript | MDN” (developer.mozilla.org). Retrieved June 19, 2023.
What is the encoding of the number
type?
The JavaScript Number type is a double-precision 64-bit binary format IEEE 754 value, like double in Java or C#. Very briefly, an IEEE 754 double-precision number uses 64 bits to represent 3 parts:
- 1 bit for the sign (positive or negative)
- 11 bits for the exponent (-1022 to 1023)
- 52 bits for the mantissa (representing a number between 0 and 1).
The mantissa (also called significand) is the part of the number representing the actual value (significant digits). The exponent is the power of 2 that the mantissa should be multiplied by.
“Number - JavaScript | MDN” (developer.mozilla.org). Retrieved June 19, 2023.
What is number
cohercion?
In JavaScript, coercion refers to the process of automatic or implicit conversion of values from one data type to another. Number coercion is when JavaScript automatically converts values to a number.
The operation can be summarized as follows:
- Numbers are returned as-is.
-
undefined
turns intoNaN
. -
null
turns into0
. -
true
turns into1
;false
turns into0
. - Strings are converted by parsing them as if they contain a number literal. Parsing failure results in
NaN
. There are some minor differences compared to an actual number literal:- Leading and trailing whitespace/line terminators are ignored.
- A leading
0
digit does not cause the number to become an octal literal (or get rejected in strict mode). -
+
and-
are allowed at the start of the string to indicate its sign. However, the sign can only appear once, and must not be followed by whitespace. -
Infinity
and-Infinity
are recognized as literals. - Empty or whitespace-only strings are converted to
0
. - Numeric separators are not allowed.
-
BigInts
throw aTypeError
to prevent unintended implicit coercion causing loss of precision. -
Symbols
throw aTypeError
. - Objects are first converted to a primitive by calling their
[@@toPrimitive]()
(with"number"
as hint),valueOf()
, andtoString()
methods, in that order. The resulting primitive is then converted to a number.
“Number coercion” (developer.mozilla.org). Retrieved June 19, 2023.
Types of numeric literals
-
Decimal: Base 10
e.g. 12, 3.14 -
Binary: Base 2
Prefix 0b or 0B
e.g. 0b1010 (10 in dec.) -
Octal (legacy syntax): Base 8
Prefix 0o or 0O
e.g. 0o12 (10 in dec.) -
Hexadecimal: Base 16
Prefix 0x or 0X
e.g. 0xA (10 in dec.) -
BigInt (Large integers)
Append n to the number
e.g. 10n -
Exponential Notation
e.g. 1.2e3 (1200)
“Numeric literals” (developer.mozilla.org). Retrieved June 19, 2023.
Numeric separators
Numeric separators in JavaScript are used to make numeric literals more readable by creating a visual separation between groups of digits.
The underscore (_
) is used as the separator.
Example:
- 1_000_000
instead of 1000000
- 0b1010_0011
instead of 0b10100011
- 0x1F_A1
instead of 0x1FA1
- 3.1415_9265
instead of 3.14159265
“Numeric literals” (developer.mozilla.org). Retrieved June 20, 2023.
What is the precission of the Number
type in JavaScript?
In JavaScript, numbers are implemented in double-precision 64-bit binary format IEEE 754 (i.e., a number between ±2^−1022
and ±2^+1023
, or about ±10^−308
to ±10^+308
, with a numeric precision of 53 bits). Integer values up to ±2^53 − 1
can be represented exactly.
In addition to being able to represent floating-point numbers, the number type has three symbolic values: +Infinity
, -Infinity
, and NaN
(not-a-number).
“Numbers” (MDN Web Docs). Retrieved February 26, 2024.
Decimal numbers
1234567890 42
Decimal literals can start with a zero (0
) followed by another decimal digit, but if all digits after the leading 0
are smaller than 8
, the number is interpreted as an octal number. This is considered a legacy syntax, and number literals prefixed with 0
, whether interpreted as octal or decimal, cause a syntax error in strict mode — so, use the 0o
prefix instead.
0888 // 888 parsed as decimal 0777 // parsed as octal, 511 in decimal
“Decimal numbers” (MDN Web Docs). Retrieved February 26, 2024.
Binary numbers
Binary number syntax uses a leading zero followed by a lowercase or uppercase Latin letter “B” (0b
or 0B
). If the digits after the 0b
are not 0
or 1
, the following SyntaxError
is thrown: "Missing binary digits after 0b"
.
0b10000000000000000000000000000000 // 2147483648 0b01111111100000000000000000000000 // 2139095040 0B00000000011111111111111111111111 // 8388607
“Binary numbers” (MDN Web Docs). Retrieved February 26, 2024.
Octal numbers
The standard syntax for octal numbers is to prefix them with 0o
. For example:
0O755 // 493 0o644 // 420
There’s also a legacy syntax for octal numbers — by prefixing the octal number with a zero: 0644 === 420
and "\045" === "%"
. If the digits after the 0
are outside the range 0
through 7
, the number will be interpreted as a decimal number.
const n = 0755; // 493 const m = 0644; // 420
Strict mode forbids this octal syntax.
“Octal numbers” (MDN Web Docs). Retrieved February 26, 2024.
Hexadecimal numbers
Hexadecimal number syntax uses a leading zero followed by a lowercase or uppercase Latin letter “X” (0x
or 0X
). If the digits after 0x
are outside the range (0123456789ABCDEF
), the following SyntaxError
is thrown: "Identifier starts immediately after numeric literal"
.
0xFFFFFFFFFFFFFFFFF // 295147905179352830000 0x123456789ABCDEF // 81985529216486900 0XA // 10
“Hexadecimal numbers” (MDN Web Docs). Retrieved February 26, 2024.