Force conversion method: 1. Use ToString(), ToNumber() or ToBoolean() method; 2. Use String(), Number() or Boolean() method; 3. Use " " or "- "operator; 4. Use statements such as "if()" and "while()".
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
1.ToString
For ordinary objects, Unless you define the toString method yourself, theObject.prototype.toString()method will be called. If the object has its own toString method, stringification will call the method and use its return value.
2.ToNumber
let a = { valueOf:function(){ return "42"; } }; let b = { toString:function(){ return "42"; } }; let c = [4,2]; c.toString = function(){ return this.join(""); //"42" }; Number(a); //42 Number(b); //42 Number(c); //42 Number(''); //0 Number([]); //0 Number(["abc"];//NaN
3.ToBoolean
Values in JavaScript can be divided into values that can be forced to false and values that can be forced Converts two types of values to true.
False value:
True value:Except for the above list, all objects can be understood as true values, and all objects are true values
let a1 = new Boolean( false ); let b1 = new Number( 0 ); let c1 = new String( "" ); let d1 = Boolean(a1 && b1 && c1); //true let a2 = []; let b2 = {}; let c2 = function(){}; let d2 = Boolean(a2 && b2 && c2); //true
1. Display conversion between strings and numbers
Strings and numbers The conversion is through the two built-in functionsString(..)andNumber(..).
In addition to String(..) and Number(..), there are other methods to achieve explicit conversion between strings and numbers:
let a = 42; let b = a.toString(); let c = "3.14"; let d = +c; b; // "42" d; // 3.14
a.toString() is explicit , but there is an implicit conversion involved. Because toString() doesn't work with primitive values like 42, the JavaScript engine automatically creates a wrapper object for 42 and then calls toString() on that object. This explicit conversion contains implicit conversion.
2. Display conversion to Boolean value
let a = "0"; let b = []; let c = {}; let d = ""; let e = 0; let f = null; let g; Boolean( a ); // true Boolean( b ); // true Boolean( c ); // true Boolean( d ); // false Boolean( e ); // false Boolean( f ); // false Boolean( g ); // false
Although Boolean(..) is explicit, it is not commonly used . The most common way to explicitly cast a type to a Boolean value is!!
let a = "0"; let b = []; let c = {}; let d = ""; let e = 0; let f = null; let g; !!a; // true !!b; // true !!c; // true !!d; // false !!e; // false !!f; // false !!g; // false
[Recommended learning:javascript advanced tutorial]
1. Implicit cast conversion between strings and numbers
( 1) The operator
can be used for both numerical addition and string concatenation
if one of the operands is a string (or If a string is obtained by calling the ToPrimitive abstract operation on it), string concatenation is performed; otherwise, numeric addition is performed. Because the array's valueOf() operation cannot get a simple primitive value, it calls toString() instead. So the two arrays in the example below become "1,2" and "3,4". Concatenating them returns "1,23,4".
let a = "42"; let b = "0"; a + b; // "420" let c = 42; let d = 0; c + d; // 42 let x = [1,2]; let y = [3,4]; x + y; // "1,23,4"
let a = 42; let b = a + ""; b; // "42"
According to the ToPrimitive abstract operation rules, a "" will call the valueOf() method on a, and then convert the return value into a string through the ToString abstract operation. String(a) calls ToString() directly. They finally return strings, but if a is an object rather than a number, the result may be different
let a = { valueOf: function() { return 42; }, toString: function() { return 4; } }; a + ""; // "42" String( a ); // "4"
-Yes Numeric subtraction operator, so a - 0 will cast a to a number. You can also use a * 1 and a /1, because these two operators only apply to numbers, but such usage is less common
let a = [3]; let b = [1]; a - b; // 2
2. Implicit mandatory type Convert to Boolean value
Implicit cast conversion of Boolean value will occur in the following situations:
(1) Conditional judgment expression in if (..) statement .
(2) The conditional expression (second) in the for ( .. ; .. ; .. ) statement.
(3) Conditional expressions in while (..) and do..while(..) loops.
(4) ? :Conditional judgment expression in .
(5) The operand on the left side of logical operators || (logical OR) and && (logical AND) (as a conditional expression).
In the above case, non-Boolean values will be implicitly cast to Boolean values, following the ToBoolean abstract operation rules introduced earlier.
For more programming-related knowledge, please visit:Introduction to Programming! !
The above is the detailed content of How to perform forced type conversion in javascript. For more information, please follow other related articles on the PHP Chinese website!