The interview the day before yesterday, the interviewer asked a super simple topic:
"True==3" returns Nothing.
I blurted out, true, without hesitation. , because there is an implicit type conversion, 3 is converted to true, so the last return is true. UEFA buy GA, I say also particularly righteously. After hanging up the phone, feel, wrong, true==3, return is false ah ... False Ah ... Because the implicit type conversion is not 3, but true is converted to 1, so 1==3, return false.
Suddenly feel that the sky is dark, so the basic and simple questions should answer wrong. So, I picked up the Little Red Book, put the third chapter carefully read it again, and sure enough to read a few times its righteousness from the see, there are new discoveries. This blog post, talk about type conversion, and then extract the knowledge that I think is very important to me.
Okay, body start 1, data types in JavaScript basic data types: Null, Undefined, String, number, Boolean complex data type: Object, including array, Date, REGEXP, Function
JavaScript variables are loosely typed variables, so the typeof operator is required to detect the data type of the given variable. (1) Undefined
When you declare a variable with VAR but it is not initialized, the value of the variable is undefined
var age;
Console.log (age);//undefined
Console.log (name);//generates an error because the name
Console.log (typeof name) is not declared;//undefined
For a variable that has not yet been declared, only one operation can be performed, and the typeof operator is used to detect its data type, returning undefined. Therefore, it is recommended that you explicitly initialize the variable so that when you return undefined using the typeof operator, you know that the variable is not declared. (2) Null
A null type is a data type with only one value, and it has only one value of NULL. From a logical point of view, a null value represents an empty object pointer, so typeof null returns object.
typeof Null;//object
If the variable you are defining is ready to be used to save the object in the future, it is best to initialize the variable to null so that you can know whether the corresponding variable has saved a reference to an object as long as you check the null value. (3) Boolean type
Boolean literals are true and false, case-sensitive. A Boolean () transformation function that converts a value to its corresponding Boolean value.
Conversion rules:
The above conversion rules are important for automating Boolean conversions of Process control statements, such as if. (4) Number type e notation: 3e3, i.e. 3*1000
Value range: holds the minimum value that ECMAScript can represent in Number.min_value; save maximum in Number.MAX_VALUE
nan:a case in which an operand that would otherwise return a numeric value does not return a numeric value (so that no error is thrown), such as if any number divided by 0 will return NaN. any operation involving Nan returns a nan,nan that is not equal to any value , by isNaN () to determine if an argument is not a numeric value
Numeric conversions, with three function number (), parseint (), parsefloat (). The latter two are specifically designed to convert a string to a numeric value.
Conversion rules:
Number () Boolean,true converted to 1,false to 0 undefined, converted to Nan; string, if it contains only numbers, converts to decimal value, null string to 0, contains 16 binary format, Convert to corresponding decimal value; object, null conversion to 0, other call valueof ()
parseint () empty string, returning Nan; other is similar to number to accept the second argument, specifying the cardinality (4) String type
Escape sequence
Feature: Once created, values cannot be changed, to change the string that a variable holds, first destroy the original string, and then populate the variable with another string containing the new value
Type converts toString () to convert a value to a string. null,undefined doesn't have this method . String (): If the value has the ToString () method, call the method and return the result, if the value is NULL, return "null", or return "undefined" if the value is undefined.
2. Explicit type conversions
With manual type conversions , JavaScript provides the following transformation functions: Converting to numeric types: Number (Mix), parseint (String,radix), parsefloat (String) Convert to String type: toString (Radix), String (Mix) Convert to Boolean type: Boolean (Mix)
Rules See 1 3, implicit type conversions
In some cases, even if we do not provide a display transformation, JavaScript will be automatically type converted, mainly:
Equality operator (= =):
The equality operator compares the action values implicitly : If an action value is a Boolean, it is converted to a numeric value before the comparison if one action value is a string and the other action value is a value, the string is converted to a numeric value by the number () function If one action value is an object and the other is not, the valueof () method of the object is invoked, and the resulting result is compared to the preceding rule null is equal to undefined if an action value is Nan, the equality comparison returns false if the two action values are objects, To compare whether they point to the same object
Relational operator (", <=, >=) If two action values are numeric, compare numeric values if the two action values are strings, the character encoding values corresponding to the string if only one action value is a numeric value, the other action value is converted to a numeric value If an operand is an object, call the ValueOf () method (the ToString () method is invoked if the object does not have a valueof () method), and the resulting result is compared to the previous rule if an action value is a Boolean, it is converted to a numeric value and then compared
Note: Nan is a very special value that is not equal to any type of value, including itself, and returns false when it is compared to any type of value.
So, in the first question, because the number of operands in the comparison is numeric, the other operand is converted to a numeric value for comparison
Look at the first three chapters of Little Red Book: Uninitialized values are saved as undefined alert (null = undefined),//true parseint () converts the empty string returns Nan, and post increments and decrements are performed after the statement containing them is evaluated
var a = 2;
var b = 3;
var c = a--+ b;//c = 5;
5. Applying a unary plus operation to a non-numeric value is equivalent to performing a number () transformation function;
6. NaN returns true;
7. Equality operations first convert and compare, and equality operations do not convert direct comparisons