This is the 31st day of my participation in the August Text Challenge.More challenges in August
Number
- NaN is not a significant number, but is of type number
- It’s not equal to any value
- IsNaN ([val]) converts value implicitly to a Number to see if it is not a valid Number. If the value is not a valid Number, return true. If the value is a Number, return false
- To output a value based on alert, the value is implicitly converted to a string.
1. Convert other data types to number
- Number
-
(Number([val]),parseInt(),parseFloat()))
-
Implicit conversions are performed based on the Number
- isNaN([val])
- Mathematical operation special case + is string concatenation
- When == is compared, some values need to be converted to numbers for comparison
-
To convert a string to a number, all characters in the string must be valid digits
- Number(“”) => 0 Number(“10”) =>10 Number(“10px”)=> NaN
-
Number(true) =>1 Number(false) =>0
-
Null => 0 Number(undefined) => NaN
-
Symbol cannot be converted to numbers
-
To convert an object or function to a number, first convert the object to a string based on toString, and then convert the string to a number
- parseInt
- The value is converted to a string, and then the first character to the left of the string is searched. All valid digits found are converted to numbers, and the search is stopped until a non-valid digit character is encountered. = > 10 parseInt (” 10.5 px “); = > 10 parseFloat (” 10.5 px “) = > 10.5
- parseFloat
- ParseFloat simply recognizes one more decimal point than parseInt
Convert other data types to strings
-
toString
-
String
-
Implicit conversion
- Implicit conversions generally call toString
- In the + operation, if the + side produces a string, it is not a mathematical operation, but a string concatenation
- To convert an object to a number, toString() is converted to a string and then to a number.
- These output methods, based on Alert Confirm Prompt document.write, convert content to string and other types to Boolean
-
! The conversion to a Boolean type is reversed
-
!!!!! Convert to a Boolean type
-
Boolean ([val])
-
In loops or conditional judgments, the result of conditional processing is a Boolean type
-
Only “”,0,undefined,null,NaN will become false, the rest will be true
console.log(Number({0:10})); Consoel.log (Number([10])); Console. log(Number([10,20])) toString results in 10,20 with an invalid characterCopy the code
Plus sign in the Js is not necessarily a mathematical operation, as long as the side of a string (or object because digital + object, the theory is put objects into a mathematical operation, but we know that the object into a child, want to call the toString method first, first converted to a string, in converted to strings, met a plus sign and turned into a string concatenation)
parseInt("") > NaN Number("") > 0 isNaN("") > false Number(null) > 0 isNaN(null) >false isNaN(Number(!! Number (parseInt (0.8)))) > false typeof! parseInt(null) + ! IsNaN (null)==== calculates typeof and plusCopy the code
Detection data type
-
typeof
- The result of the test is a string containing the corresponding data type
- Typeof NULL returns “object”
- Detection array, re, date, object return “object”, so cannot be determined based on this method
-
instanceof
-
constructor
-
Object.prototype.toString.call([val])
There are two kinds of comparison operations in JS: == comparison
- Several special points of the same type
- The {} == {} => false object compares the address of heap memory
- [] == [] false; NaN == NaN false
- A few special points of different types
- Null == undefined true but === false because the type is different, leaving null/undefined is not equal to any other data type
- The string == object is converted to a string
- If the data types on both sides of == are different, they need to be converted to numbers for comparison
console.log(2 == true); Console. log([] == false) converts both data values to numbers and compares console.log(! [] == false [] => false
[] = =! [] What was the result? Why is that? ==, both sides need to be converted to numbers and then compared. [] converts to a number 0. ! [] is first converted to a Boolean. Since [] is a reference type converted to a Boolean value of true, therefore! [] is f alse, which then converts to a number and becomes 0. 0 == 0, the result is true console.log({a: 1} == true); // false console.log({a: 1} == “[object Object]”); // true
({}+{}).length {}+10 =>10 ({}+{}).length {}+10 =>10 ({}+{}).length {}+10 =>10