Since I haven't learned any other programming languages, as my first programming "native language", I won't give examples of other programming languages here. JavaScript, as a variable of dynamic type scripting language, has no type, so how can we convert its variables? And forced conversion.
JavaScript variables really have no type. That is to say, after you declare variables with var, you can assign values to variables at will without any problems. But the values assigned to variables themselves are of type, such as String, Object, Boolean, Number, etc. So JavaScript variables still have type conversion. Of course, most of the time, JavaScript parsing engine will default to handle a lot of type conversion. However, sometimes you need to forcibly convert the data type, the most common is: "" + Number + Number, which will generate String accumulation.
- The Number function, which can cast numeric strings, objects, and Boolean variables to numeric.
- Object function, which can cast numeric string, number, and boolean variable to object type.
- String function, which can cast numbers, objects, and Boolean variables into string type.
- Boolean function, which can cast numeric string, object, and numeric variable to Boolean.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>JavaScript Cast in</title> </head> <body> <center> <h1>JavaScript Cast in</h1> <hr> <br> <h5>The following change information is displayed</h5> <pre> <script> var str = '100';//Declare a string variable str,The initial value is "100" var num = Number(str);//use number Cast will str Convert to numeric type and assign to num variable document.writeln(typeof(num) + ':' + num);//Printing num Type and value of var obj = Object(str);//use object Cast will str Convert to object type and assign to obj variable document.writeln(typeof(obj) + ':' + obj);//Printing obj Type and value of var bool = Boolean(str);//use Boolean Cast will str Convert to boolean type and assign to bool variable document.writeln(typeof(bool) + ':' + bool);//Printing bool Types and variables for document.writeln(); var num = 100 ;//Declare a string variable num,The initial value is 100 var str = String(num);//use string Cast will num Convert to character type and assign to str variable document.writeln(typeof(str) + ':' + str); var bool = Boolean(num); document.writeln(typeof(bool) + ':' + bool); var obj = Object(num); document.writeln(typeof(obj) + ':' + obj); document.writeln(); var bool = true; var str = String(bool); document.writeln(typeof(str) + ':' + str); var num = Number(bool); document.writeln(typeof(num) + ':' + num); var obj = Object(bool); document.writeln(typeof(obj) + ':' + obj); document.writeln; var obj = {}; var str = String(obj); document.writeln(typeof(str) + ':' + str); var num = Number(obj); document.writeln(typeof(num) + ':' + num); var bool = Boolean(obj); document.writeln(typeof(bool) + ':' + bool); </script> </pre> </center> </body> </html>