Other data types, such as numbers, Booleans, and so on, can be converted to strings; In general, the scripting engine automatically completes such conversions based on context. For example, when a numeric or Boolean variable is passed to a function that expects to receive a string variable, the value is implicitly converted to a string and then processed:
var num_value=35.00; alert (num_value); // expected as a string
In addition, if you want to perform an addition operation on two variables in an assignment statement, where one is a string variable and the other is a numeric variable, the number variable is automatically converted to a string, followed by the two strings:
var num_value=35.00; var string_value= "This is a number:" +num_value;
When to convert a number to a string depends on when the JavaScript scripting engine handles the string. For example, if a string is the first of a sequence value, all values are treated as strings:
var strvalue= "4" + 3 + 1; // The result is "431." var strvalue=4 + 3 + "1"; // The result is
However, if you use a different action symbol (in addition to the plus sign), the string is converted to a number:
var firstresult= "3"; // subtraction operation, the result is var secondresult= "3"; // multiplication operation, the result is 9 var thirdresult=30/"3"; // division operation, the result is ten
Implicit conversions depend on the position of the operator and the variable, which more fully reflects the danger of a loose type: The value varies with context, depending on the order in which new data type operations are introduced, and the operators that are referenced.
string conversion of JavaScript