JS has six big data types: String,number,boolean,null,undefined,object. The first five data types are referred to as primitive types.
The typeof operator returns a value of "string", "Number", "Boolean", "Undefined", "object", "function". Where the return value of a null type is "Object" and function is a subclass of type object, the result is "function"
Non-numeric types are converted to numeric type rules
//an empty string is converted to 0Console.log (Number (""));//0//string contains only numbers, returns the decimal numberConsole.log (Number ("19"));// +//The string is in hexadecimal form and returns an equal decimalConsole.log (Number ("0x19"));// -//string is a floating-point form, returning a floating-point numberConsole.log (Number ("12.5"));//12.5//string is a floating-point form, returning a floating-point numberConsole.log (Number ("1.234e2"));//123.4//returns nan in addition to the string form aboveConsole.log (Number ("a"));//NaN//Boolean true to return 1Console.log (Number (true));//1//Boolean false returns 0Console.log (Number (false));//0//number form, return the original parameterConsole.log (number (23));// at//null type returns 0Console.log (Number (NULL));//0//undefined type returns nanConsole.log (number (undefined));//NaN
Object type conversions have special rules
When the object type is strongly converted to number type, the object's valueof () method is called first, if the return value of the method is the original type, then number (the return value of the valueof), if the return type of the method is the object type, The ToString () method of the object is then called further. If the return value of the ToString () method is the original type, number (the return value of ToString), and if the return value of the ToString () method is of type object, then the final error is
<Script> varobj={name:"PMX", Age: -, ValueOf:function() {Console.log ("value of"); return This. Name; }, ToString:function() {Console.log ('To string'); return This. Name; } }</Script>
The result of number (' Pmx ') is Nan
<Script>varobj={name:"PMX", Age: -, ValueOf:function() {Console.log ("value of"); return NULL}, ToString:function() {Console.log ('To string'); return This. Name; }}</Script>
Number (NULL) is the result of 0
<Script>varobj={name:"PMX", Age: -, ValueOf:function() {Console.log ("value of"); return{}}, toString:function() {Console.log ('To string'); return This. Name; }}</Script>
ValueOf () returns a non-primitive type, then calls the ToString () method of obj, number (' pmx ') result is Nan
<Script>varobj={name:"PMX", Age: -, ValueOf:function() {Console.log ("value of"); return{}}, toString:function() {Console.log ('To string'); return {}; }}</Script>
The return result of ToString () is the object type, and the last number cast fails with an error.
<Script>varobj={name:"PMX", Age: -, ValueOf:function() {Console.log ("value of"); return{}}, toString:function() {Console.log ('To string'); return{A:Ten, ValueOf:function(){ return Ten; }, ToString:function(){ return 'a' } }; }}</Script>
The ToString () method returns an object that customizes the valueof () method and the ToString () method, still error, indicating that the numeric strong turn does not recursively
JS Strong turn