Typeof Data type detection
typeof [value]
- The return result is a “string” containing the corresponding data type.
- “number”/”string”/”boolean”/”undefined”/”symbol”/”bigint”/”object”/”function”.
- Limitations (defects):
- typeof null => “object”
- Typeof object/array/re/date… => “object” cannot subdivide object types based on Typeof.
- Detection principle:
-
All data type values are stored in the computer as binary (0/1) values.
-
Binary values for object data types start with 000 (this is true for both ordinary objects and array regular objects).
-
Null The binary value stored in the calculation :000.
-
Typeof checks the data type based on its binary value. If the value starts with 000, it is considered an object. Therefore, if null is detected, it is considered an object and “object” is returned.
-
console.log(typeof typeof [10, 20]); // =>"string" // typeof [10, 20] -> "object" // typeof "object" -> "string" function fn(x) => x===undefined {x= 0; } if (typeof x == "undefined") {x = 0;} if (typeof x == "undefined"); } } fn();Copy the code
“= =” and “= = =”
-
== : Equals. Compared with absolute equals, if the browser finds that the two types are different, the browser will implicitly convert the two types to the same type and then compare them
- NaN = = NaN is not equal
- Null ==undefined is equal null===undefined is not equal NULL /undefined is not equal to any other value
- If a string == object is used, the object is converted to a string and then compared.
- The remaining two sides of the data type is different, according to the “convert to a number and then compare” rule processing and then compare.
-
=== : absolute equality (recommended), which requires both data types and values to be equal. If one is not equal, both results are false (there is no implicit conversion of data types).
console.log(10 == "10"); Console. log(10 === "10"); // True converts "10" to the number 10 and compares it. //false console.log("10"==[10]); Console. log("1"==true); Console. log(0==[]); console.log(0==[]); // True converts [] to string "" and then to number 0 for comparison.Copy the code
Requirement: Verify that obj is an object
var obj = {}; // This does not work because typeof null is also "object" and NULL is not an object. If (typeof obj === "object") {console.log('obj is an object~~'); } // this is the way to write, //! ==: belong to absolutely not equal, == correspond! =(not equal to), === correspond! If (obj! == null && typeof obj === "object") { console.log('obj is an object~~'); }Copy the code
Requirement: sum of any number
var args = arguments; Var total = 0; var total = 0; for (var i = 0; i < args.length; I ++) {// I =0; // I =1; // I =1; // I =1; // I =1; // I =1; // I =1; The args every item of this collection is also starting index 0, = > each round loop, I stored value at the right moment is that we need to get the current item in the set index = > each round loop args [I] to obtain the current round of cycle, from collection to remove the current item, Var item = Number(args[I]); // Item is the current value of each loop. If (! //if(isNaN(item) === false) //isNaN(item) === false {total += item; } } return total; } var res = sum(10, 20); console.log(res); res = sum(10, 20, '30'); console.log(res); res = sum(10, 20, '30', 'AA'); console.log(res); Var sum = (... args) => { var total = 0; for (var i = 0; i < args.length; i++) { var item = Number(args[i]); if (! isNaN(item)) { total += item; } } return total; }; var res = sum(10, 20); console.log(res); res = sum(10, 20, '30'); console.log(res); res = sum(10, 20, '30', 'AA'); console.log(res);Copy the code
Sum over any number
The first method:
function sum() { var args = arguments; Var total = 0; var total = 0; for (var i = 0; i < args.length; I ++) {// I =0; // I =1; // I =1; // I =1; // I =1; // I =1; // I =1; The args every item of this collection is also starting index 0, = > each round loop, I stored value at the right moment is that we need to get the current item in the set index = > each round loop args [I] to obtain the current round of cycle, from collection to remove the current item, Var item = Number(args[I]); // Item is the current value of each loop. If (! //if(isNaN(item) === false) //isNaN(item) === false {total += item; } } return total; } var res = sum(10, 20); console.log(res); res = sum(10, 20, '30'); console.log(res); res = sum(10, 20, '30', 'AA'); console.log(res);Copy the code
The result is:
Second: change to arrow function
var sum = (... args) => { var total = 0; for (var i = 0; i < args.length; i++) { var item = Number(args[i]); if (! isNaN(item)) { total += item; } } return total; }; var res = sum(10, 20); console.log(res); res = sum(10, 20, '30'); console.log(res); res = sum(10, 20, '30', 'AA'); console.log(res);Copy the code
The result is:
Third: the use of array method to achieve summation
var sum = (... Args) => {var total = 0; args.forEach(item => { item = Number(item); if (! isNaN(item)) { total += item; }}); return total; }; var res = sum(10, 20); console.log(res); res = sum(10, 20, '30'); console.log(res); res = sum(10, 20, '30', 'AA'); console.log(res);Copy the code
The result is:
Fourth: sum based on reduce
var sum = (... args) => { return args.reduce((total, item) => { item = Number(item); isNaN(item) ? item = 0 : null; // If the item meets the isNaN(item) condition (that is, it is not a significant digit), then the item =null equals nothing. Return total + item; }, 0); }; var res = sum(10, 20); console.log(res); res = sum(10, 20, '30'); console.log(res); res = sum(10, 20, '30', 'AA'); console.log(res);Copy the code
The results for