In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of how to express the ES6 value, the content is detailed and easy to understand, the operation is simple and fast, and it has a certain reference value. I believe you will gain something after reading this article on how to express the ES6 value. Let's take a look.
ES6, the full name of ECMAScript 6.0, is the next version of JavaScript standard, 2015.06 release.
Representation of numerical value
New binary representation: prefix 0b or 0B.
Console.log (0b11 = 3); / / trueconsole.log (0B11 = 3); / / true
New octal representation: prefix 0o or 0O.
Console.log (0o11 = 9); / / trueconsole.log (0O11 = 9); / / true
Constant
Number.EPSILON
The Number.EPSILON property represents the difference between 1 and the minimum floating-point number greater than 1.
Its value is close to 2.2204460492503130808472633361816E-16, or 2-52.
Test whether the value is within the range of error:
0. 1 + 0. 2 = = 0. 3; / / false// is regarded as equal within the error range equal = (Math.abs (0. 3-0. 2) < Number.EPSILON); / / true
Attribute property
Writable:falseenumerable:falseconfigurable:false
Maximum / minimum safe integer
Safe integer
Safe integers represent integers that can be accurately represented in JavaScript. Safe integers range from-53 of 2 to 53 of 2 (excluding two endpoints). Integers beyond this range cannot be accurately represented.
Maximum safe integer
The upper limit of the range of safe integers, that is, 2 to the power of 53 minus 1.
Number.MAX_SAFE_INTEGER + 1 = = Number.MAX_SAFE_INTEGER + 2; / / trueNumber.MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER + 1; / / falseNumber.MAX_SAFE_INTEGER-1 = Number.MAX_SAFE_INTEGER-2; / / false
Minimum safe integer
The lower limit of the range of safe integers, that is, the negative number of 2 to the power of 53 minus 1.
Number.MIN_SAFE_INTEGER + 1 = = Number.MIN_SAFE_INTEGER + 2; / / falseNumber.MIN_SAFE_INTEGER = Number.MIN_SAFE_INTEGER-1; / / falseNumber.MIN_SAFE_INTEGER-1 = Number.MIN_SAFE_INTEGER-2; / / true
Attribute property
Writable:falseenumerable:falseconfigurable:false
Method
New method of Number object
Number.isFinite ()
Used to check whether a value is finite (finite), that is, not Infinity
Console.log (Number.isFinite (1)); / / trueconsole.log (Number.isFinite (0.1)); / / true / / NaN is not a finite console.log (Number.isFinite (NaN)); / / falseconsole.log (Number.isFinite (Infinity)); / / falseconsole.log (Number.isFinite (- Infinity)); / / false / / Number.isFinate has no implicit Number () type conversion, all non-numeric values return falseconsole.log (Number.isFinite ('foo')) / / falseconsole.log (Number.isFinite ('15')); / / falseconsole.log (Number.isFinite (true)); / / falseNumber.isNaN () is used to check whether a value is NaN. Console.log (Number.isNaN (NaN)); / / trueconsole.log (Number.isNaN ('true'/0)); / / true / / in the global isNaN (), the following returns true, because non-numeric values are converted to numeric values before judgment / / and there is no implicit Number () type conversion for Number.isNaN (), and all non-NaN returns falseNumber.isNaN ("NaN"); / / falseNumber.isNaN (undefined) / / falseNumber.isNaN ({}); / / falseNumber.isNaN ("true"); / / false
Method of migrating from global to Number object
Gradually reduce the global method for the modularization of global variables.
The behavior of the method has not changed.
Number.parseInt ()
An integer used to convert a given string to a specified base.
/ / defaults to decimal Number.parseInt ('12.34') when no base is specified; / / 12Number.parseInt (12.34); / 12 / / specifies binary Number.parseInt ('0011 parseInt (2)); / / 3 / / is the same function as the global parseInt () function Number.parseInt = parseInt; / / trueNumber.parseFloat () to parse a string into a floating-point number. Number.parseFloat ('123.45') / / 123.45Number.parseFloat (' 123.45abc') / / 123.45 / / cannot be parsed into a floating-point number, then return NaNNumber.parseFloat ('abc') / / NaN / / the same method as the global parseFloat () method Number.parseFloat = parseFloat / / trueNumber.isInteger () to determine whether the given parameter is an integer. Number.isInteger (value) Number.isInteger (0); / / inside true// JavaScript, integers and floating point numbers are stored in the same way, so 1 and 1 are considered the same values Number.isInteger (1); / / trueNumber.isInteger (1.0); / / trueNumber.isInteger (1. 1); / / falseNumber.isInteger (Math.PI); / / false / / NaN and positive and negative Infinity are not integers Number.isInteger (NaN) / / falseNumber.isInteger (Infinity); / / falseNumber.isInteger (- Infinity); / / falseNumber.isInteger ("10"); / / falseNumber.isInteger (true); / / falseNumber.isInteger (false); / / falseNumber.isInteger ([1]) / / false / / when the accuracy of the value exceeds 53 binary bits, because the 54th bit and the following bits are discarded, there will be a misjudgment that the absolute value of Number.isInteger (1.0000000000000001) / / true / / is less than Number.MIN_VALUE (5E-324), that is, less than the minimum value that JavaScript can distinguish / /, it will be automatically converted to 0, and misjudgment Number.isInteger (5E-324) will also occur; / / falseNumber.isInteger (5E-325) / / trueNumber.isSafeInteger () is used to determine whether the value is within the safe range. Number.isSafeInteger (Number.MIN_SAFE_INTEGER-1); / / falseNumber.isSafeInteger (Number.MAX_SAFE_INTEGER + 1); / / false
Extension of Math object
ES6 adds 17 mathematically related static methods to the Math object, which can only be called in Math.
General calculation
Math.cbrt
Used to calculate the cubic root of a number.
Math.cbrt (1); / / 1Math.cbrt (0); / / 0Math.cbrt (- 1); / /-1max / will convert non-numeric values to Math.cbrt ('1'); / / 1pm / non-numeric values and cannot be converted to numeric values return NaNMath.cbrt ('hhh'); / / NaNMath.imul
The result of multiplying two numbers as a 32-bit signed integer also returns a 32-bit signed integer.
/ / in most cases, the result is the same as a * b Math.imul (1, 2); / / 2 Math.imul / is used to correctly return the low value Math.imul (0x7fffffff, 0x7fffffff) in the result of multiplication of large numbers; / / 1Math.hypot
Used to calculate the square root of the sum of squares of all parameters.
Math.hypot (3,4); / / 5 / / non-numeric values are first converted to numeric values and then calculated Math.hypot (1,2,'3'); / / 3.741657386773941Math.hypot (true); / / 1Math.hypot (false); / / 0 / / null values are converted to 0Math.hypot (); / / 0Math.hypot ([]); / / 0 / / the parameter Infinity or-Infinity returns InfinityMath.hypot (Infinity) / / InfinityMath.hypot (- Infinity); / / Infinity / / returns NaNMath.hypot (NaN) if there are parameters in the parameter that cannot be converted to numeric values; / / NaNMath.hypot (3,4, 'foo'); / / NaNMath.hypot ({}); / / NaNMath.clz32
The number of leading zeros in the form of a 32-bit unsigned integer used to return a number.
Math.clz32 (0); / / 32Math.clz32 (1); / / 31Math.clz32 (0b01000000000100000000000000000000); / / 1 / / when the parameter is a decimal, only the integer part Math.clz32 (0.5) is considered; / / 32 / / for null or non-numeric values, it is converted to a numeric value and then calculated Math.clz32 ('1'); / / 31Math.clz32 (); / / 32Math.clz32 ([]) / / 32Math.clz32 ({}); / / 32Math.clz32 (NaN); / / 32Math.clz32 (Infinity); / / 32Math.clz32 (- Infinity); / / 32Math.clz32 (undefined); / / 32Math.clz32 ('hhh'); / / 32
Digital processing
Math.trunc
Used to return the integer portion of a number.
Math.trunc (12.3); / / 12Math.trunc (12); / / 12 / / the symbol Math.trunc (- 0.5) is also determined when the integer part is 0; / /-0Math.trunc (0.5); / / 0 / / Math.trunc converts a non-numeric value to a numeric value and then processes Math.trunc ("12.3"); / / 12 / / returns NaNMath.trunc () when a null value or cannot be converted to a numeric value / / NaNMath.trunc (NaN); / / NaNMath.trunc ("hhh"); / / NaNMath.trunc ("123.2hhh"); / / NaNMath.fround
The 32-bit single-precision floating-point form used to get numbers.
/ / if you take an integer from minus to the 24th power of 2 (excluding two endpoints), the return result is consistent with the parameter itself, Math.fround (- (2percent 24) + 1); / /-16777215Math.fround (2percent 24-1) / / 16777215 / / used to convert 64-bit double-precision floating-point numbers to 32-bit single-precision floating-point numbers Math.fround (1.234) / / 1.125 big / when the precision of the decimal exceeds 24 binary digits, the precision Math.fround (0.3) will be lost / / 0.30000001192092896 NaN / return its own Math.fround (NaN) / / NaNMath.fround (Infinity) / / Infinity / / if the parameter is NaN or Infinity, the parameter will be converted to Math.fround ('5'); / / 5Math.fround (true); / / 1Math.fround (null); / / 0Math.fround ([]); / / 0Math.fround ({}); / / NaN if the parameter is other non-numeric type
Judge
Math.sign
The symbol that determines the number (positive, negative, 0).
When Math.sign (1); / / 1Math.sign (- 1); / /-1 / / parameter is 0, different symbols return different Math.sign (0); / / 0Math.sign (- 0); / /-0 / / convert non-numeric values before judging Math.sign ('1'); / / 1Math.sign ('- 1') / /-1 / / return NaNMath.sign (NaN) if the parameter is non-numeric (cannot be converted to numeric value); / / NaNMath.sign ('hhh'); / / NaN
Logarithmic method
Math.expm1 ()
The result used to calculate the x power of e minus 1, that is, Math.exp (x)-1.
Math.expm1 (1); / / 1.718281828459045Math.expm1 (0); / / 0Math.expm1 (- 1); / /-0.6321205588285577max / will convert non-numeric values to Math.expm1 ('0'); return NaNMath.expm1 (NaN) if the parameter is not numeric and cannot be converted to numeric values; / / NaNMath.log1p (x)
Used to calculate the natural logarithm of 1 + x, that is, Math.log (1 + x).
Math.log1p (1); / / 0.6931471805599453Math.log1p (0); / / 0Math.log1p (- 1); / /-Infinity / / returns NaNMath.log1p (- 2) if the parameter is less than-1; / / NaNMath.log10 (x)
Used to calculate the logarithm of x with a base of 10.
Math.log10 (1); / / 0 Infinity// / convert non-numeric values before calculation: NaNMath.log10 ('1'); / / 0 NaNMath.log2 / return-InfinityMath.log10 (0) if the parameter is 0; / / return NaNMath.log10 (- 1) if the parameter is less than 0 or is not numeric (and cannot be converted to a numeric value); / / NaNMath.log2 ()
Used to calculate the logarithm of x with a base of 2.
Math.log2 (1); / / 0 Infinity// / convert non-numeric values before calculation: NaNMath.log2 ('1'); / / 0 NaN / return-InfinityMath.log2 (0) if the parameter is 0; / / return NaNMath.log2 (- 1) if the parameter is less than 0 or is not numeric (and cannot be converted to a numeric value); / / NaN
Hyperbolic function method
Math.sinh (x): used to calculate hyperbolic sinusoids.
Math.cosh (x): used to calculate hyperbolic cosine.
Math.tanh (x): used to calculate hyperbolic tangents.
Math.asinh (x): used to calculate the inverse hyperbolic sine.
Math.acosh (x): used to calculate the inverse hyperbolic cosine.
Math.atanh (x): used to calculate the inverse hyperbolic tangent.
Exponential operator
1 * * 2; / / 1 ES6 / right combination, calculate 2 * * 2 * * 3 from right to left; / / 256 * * / 2 * * = let exam = 2 * * = 2 * * = 2 * * 2; / / 4 this is the end of the article on "how to express ES6 values". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to express ES6 numbers". If you want to learn more knowledge, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.