Easy Tutorial
❮ Android Tutorial Contentprovider Front End Developer Questions And Answers ❯

3.2.2 ES6 Numerical Values

Category ES6 Tutorial

Representation of Numerical Values

New notation for binary representation: prefix 0b or 0B.

console.log(0b11 === 3); // true
console.log(0B11 === 3); // true

New notation for octal representation: prefix 0o or 0O.

console.log(0o11 === 9); // true
console.log(0O11 === 9); // true

Constants

Number.EPSILON

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

Its value is close to 2.2204460492503130808472633361816E-16, or 2^-52.

Testing whether a numerical value is within the error range:

0.1 + 0.2 === 0.3; // false
// Considered equal if within the error range
equal = (Math.abs(0.1 - 0.3 + 0.2) < Number.EPSILON); // true

Property Attributes

writable: false
enumerable: false
configurable: false

Maximum/Minimum Safe Integers

Safe Integers

Safe integers represent integers that can be precisely represented in JavaScript, with the safe integer range between 2^-53 and 2^53 (excluding the endpoints). Integers beyond this range cannot be represented precisely.

Maximum Safe Integer

The upper limit of the safe integer range, which is 2^53 minus 1.

Number.MAX_SAFE_INTEGER + 1 === Number.MAX_SAFE_INTEGER + 2; // true
Number.MAX_SAFE_INTEGER === Number.MAX_SAFE_INTEGER + 1;     // false
Number.MAX_SAFE_INTEGER - 1 === Number.MAX_SAFE_INTEGER - 2; // false

Minimum Safe Integer

The lower limit of the safe integer range, which is the negative of 2^53 minus 1.

Number.MIN_SAFE_INTEGER + 1 === Number.MIN_SAFE_INTEGER + 2; // false
Number.MIN_SAFE_INTEGER === Number.MIN_SAFE_INTEGER - 1;     // false
Number.MIN_SAFE_INTEGER - 1 === Number.MIN_SAFE_INTEGER - 2; // true

Property Attributes

writable: false
enumerable: false
configurable: false

Methods

New Methods of the Number Object

Number.isFinite()

Used to check if a numerical value is finite, i.e., not Infinity.

console.log( Number.isFinite(1));   // true
console.log( Number.isFinite(0.1)); // true

// NaN is not finite
console.log( Number.isFinite(NaN)); // false

console.log( Number.isFinite(Infinity));  // false
console.log( Number.isFinite(-Infinity)); // false

// Number.isFinite does not have implicit Number() type conversion, all non-numerical values return false
console.log( Number.isFinite('foo')); // false
console.log( Number.isFinite('15'));  // false
console.log( Number.isFinite(true));  // false
Number.isNaN()
Used to check if a value is NaN.
console.log(Number.isNaN(NaN));      // true
console.log(Number.isNaN('true'/0)); // true

// In the global isNaN(), the following all return true because they are converted to numerical values before being evaluated
// Number.isNaN() does not have implicit Number() type conversion, all non-NaN values return false
Number.isNaN("NaN");      // false
Number.isNaN(undefined);  // false
Number.isNaN({});         // false
Number.isNaN("true");     // false

Methods Transferred from Global to the Number Object

Gradually reducing global methods for modularization of global variables.

The behavior of the methods remains unchanged.

Number.parseInt()

Used to convert a given string into an integer in a specified base.

// Default to base 10 if not specified
Number.parseInt('12.34'); // 12
Number.parseInt(12.34);   // 12

// Specify base
Number.parseInt('0011',2); // 3

// The same function as the global parseInt()
Number.parseInt === parseInt; // true
Number.parseFloat()
Used to parse a string into a floating-point number.
Number.parseFloat('123.45')    // 123.45
Number.parseFloat('123.45abc') // 123.45

// Returns NaN if
Math.trunc(-0.5); // -0
Math.trunc(0.5);  // 0

// Math.trunc converts non-numeric values to numbers before processing
Math.trunc("12.3"); // 12

// Returns NaN when the value is null or cannot be converted to a number
Math.trunc();           // NaN
Math.trunc(NaN);        // NaN
Math.trunc("hhh");      // NaN
Math.trunc("123.2hhh"); // NaN


Math.fround


Used to obtain the 32-bit single-precision floating-point number form of a number.


// For integers between -2^24 and 2^24 (excluding the endpoints), the result is consistent with the argument itself Math.fround(-(2*24)+1); // -16777215 Math.fround(2 * 24 - 1); // 16777215

// Converts a 64-bit double-precision floating-point number to a 32-bit single-precision floating-point number Math.fround(1.234) // 1.125 // When the precision of the decimal exceeds 24 binary places, precision is lost Math.fround(0.3); // 0.30000001192092896 // Returns itself when the argument is NaN or Infinity Math.fround(NaN) // NaN Math.fround(Infinity) // Infinity

// Converts the argument when it is another non-numeric type Math.fround('5'); // 5 Math.fround(true); // 1 Math.fround(null); // 0 Math.fround([]); // 0 Math.fround({}); // NaN



### Determination


Math.sign


Determines the sign of a number (positive, negative, zero).


Math.sign(1); // 1 Math.sign(-1); // -1

// Returns different results for 0 with different signs Math.sign(0); // 0 Math.sign(-0); // -0

// Converts non-numeric values before determining Math.sign('1'); // 1 Math.sign('-1'); // -1

// Returns NaN when the argument is a non-numeric value (and cannot be converted to a number) Math.sign(NaN); // NaN Math.sign('hhh'); // NaN



### Logarithmic Methods


Math.expm1()


Used to calculate the result of e to the power of x minus 1, that is, Math.exp(x) - 1.


Math.expm1(1); // 1.718281828459045 Math.expm1(0); // 0 Math.expm1(-1); // -0.6321205588285577 // Converts non-numeric values Math.expm1('0'); //0

// Returns NaN when the argument is not a number and cannot be converted to a number Math.expm1(NaN); // NaN



Math.log1p(x)


Used to calculate the natural logarithm of 1 + x, that is, Math.log(1 + x).


Math.log1p(1); // 0.6931471805599453 Math.log1p(0); // 0 Math.log1p(-1); // -Infinity

// Returns NaN when the argument is less than -1 Math.log1p(-2); // NaN



Math.log10(x)


Used to calculate the logarithm of x with base 10.


Math.log10(1); // 0 // Converts non-numeric values before calculation Math.log10('1'); // 0 // Returns -Infinity when the argument is 0 Math.log10(0); // -Infinity // Returns NaN when the argument is less than 0 or the argument is not a number (and cannot be converted to a number) Math.log10(-1); // NaN



Math.log2()


Used to calculate the logarithm of x with base 2.


Math.log2(1); // 0 // Converts non-numeric values before calculation Math.log2('1'); // 0 // Returns -Infinity when the argument is 0 Math.log2(0); // -Infinity // Returns NaN when the argument is less than 0 or the argument is not a number (and cannot be converted to a number) Math.log2(-1); // NaN ```

Hyperbolic Function Methods

-

❮ Android Tutorial Contentprovider Front End Developer Questions And Answers ❯