Working with Numbers Flashcards

1
Q

What is a value?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

number values

A

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 to 2.99 * 10^8
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Is it possible to overflow a number in JavaScript?

A

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 .

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the maximum numeric value representable in JavaScript?

A

Number.MAX_VALUE or approximately 1.8E+308

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the minimum numeric value representable in JavaScript?

A

Number.MIN_VALUE or 5E-324

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is Number.EPSILON?

A

The Number.EPSILON static data property represents the difference between 1 and the smallest floating point number greater than 1.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the encoding of the number type?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is number cohercion?

A

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 into NaN.
  • null turns into 0.
  • true turns into 1; false turns into 0.
  • 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 a TypeError to prevent unintended implicit coercion causing loss of precision.
  • Symbols throw a TypeError .
  • Objects are first converted to a primitive by calling their [@@toPrimitive]() (with "number" as hint), valueOf(), and toString() methods, in that order. The resulting primitive is then converted to a number.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Types of numeric literals

A
  1. Decimal: Base 10
    e.g. 12, 3.14
  2. Binary: Base 2
    Prefix 0b or 0B
    e.g. 0b1010 (10 in dec.)
  3. Octal (legacy syntax): Base 8
    Prefix 0o or 0O
    e.g. 0o12 (10 in dec.)
  4. Hexadecimal: Base 16
    Prefix 0x or 0X
    e.g. 0xA (10 in dec.)
  5. BigInt (Large integers)
    Append n to the number
    e.g. 10n
  6. Exponential Notation
    e.g. 1.2e3 (1200)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Numeric separators

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the precission of the Number type in JavaScript?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Decimal numbers

A
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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Binary numbers

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Octal numbers

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Hexadecimal numbers

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Number object

A

The built-in Number object has properties for numerical constants, such as maximum value, not-a-number, and infinity. You cannot change the values of these properties and you use them as follows:

const biggestNum = Number.MAX_VALUE;
const smallestNum = Number.MIN_VALUE;
const infiniteNum = Number.POSITIVE_INFINITY;
const negInfiniteNum = Number.NEGATIVE_INFINITY;
const notANum = Number.NaN;

“Number object” (MDN Web Docs). Retrieved February 26, 2024.