JavaScript mainly see Ruan Yifeng teacher’s JavaScript tutorial, this stage intends to go through quickly, mainly record some js and OC and SWIFT different key points
Integer and floating point numbers
Inside JavaScript, all numbers are stored as 64-bit floating point numbers, even integers. So, 1 and 1.0 are the same, they’re the same number.
Because floating-point numbers are not exact values, comparisons and operations involving decimals need to be particularly careful.
0.1 + 0.2= = =0.3
// false
0.3 / 0.1
/ / 2.9999999999999996
(0.3 - 0.2) = = = (0.2 - 0.1)
// false
Copy the code
Numerical range
If a number is greater than or equal to 2 to the power of 1024, then “forward overflow” will occur, i.e. JavaScript cannot represent such a large number, and Infinity will be returned.
Math.pow(2.1024) // Infinity
Copy the code
If a number is less than or equal to 2 to the -1075 power (-1023 for the exponent part, plus 52 for the decimal part), then a “negative overflow” occurs, meaning that JavaScript cannot represent such a small number and will simply return 0.
Math.pow(2, -1075) / / 0
Copy the code
NaN
NaNIs a special JavaScript value that means "Not a.Number), mainly when parsing a string into a number fails.Copy the code
NaN is not a separate datatype, but a special numeric value whose datatype is still Number, and any calculation it takes part in is NaN. For example:
5 - 'x' // NaN
Math.acos(2) // NaN
Math.log(-1) // NaN
Math.sqrt(-1) // NaN
0 / 0 // NaN
Copy the code
The arithmetic rules are similar
NaN= = =NaN // false
[NaN].indexOf(NaN) // -1
Boolean(NaN) // false
NaN + 32 // NaN
NaN - 32 // NaN
NaN * 32 // NaN
NaN / 32 // NaN
Copy the code
parseInt()
It is used to convert a string to an integer. If it is not a string, it is converted first to a string and then to an integer. Two arguments can be passed. The second argument represents a base (between 2 and 36).
parseFloat()
Used to convert a string to a floating point. ParseInt = parseInt; parseInt = parseInt;
parseFloat(true) // NaN
Number(true) / / 1
parseFloat(null) // NaN
Number(null) / / 0
parseFloat(' ') // NaN
Number(' ') / / 0
parseFloat('123.45 #') / / 123.45
Number('123.45 #') // NaN
Copy the code
isNaN()
NaN is used to determine if a value is NaN. It is valid only for numeric values. If any other value is passed in, it will be converted to a numeric value first.
- Pass string
isNaN('Hello') // true
/ / equivalent to
isNaN(Number('Hello')) // true
Copy the code
- Pass in objects and arrays
isNaN({}) // true
/ / is equivalent to
isNaN(Number({})) // true
isNaN(['xzy']) // true
/ / is equivalent to
isNaN(Number(['xzy'])) // true
isNaN([]) // false
isNaN([123]) // false
isNaN(['123']) // false
Copy the code
Note that an empty array or an array with only one numeric element will be false when passed, because these arrays can be converted to numeric values by the Number function. NaN is not equal to its own value.
function myIsNaN(value) {
returnvalue ! == value; }Copy the code
isFinite()
The method returns a Boolean value indicating whether a value is normal. IsFinite returns true for all values except Infinity, -infinity, NaN, and undefined, which return false
isFinite(Infinity) // false
isFinite(-Infinity) // false
isFinite(NaN) // false
isFinite(undefined) // false
isFinite(null) // true
isFinite(-1) // true
Copy the code