源代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>享学课堂</title> </head> <body> <p>删除注释(在行的开头)以测试每种情况:</p> <p id="demo"></p> <script> var x = 5; //x = 5 + 7; // x.valueOf() is 12, typeof x is a number //x = 5 + "7"; // x.valueOf() is 57, typeof x is a string //x = "5" + 7; // x.valueOf() is 57, typeof x is a string //x = 5 - 7; // x.valueOf() is -2, typeof x is a number //x = 5 - "7"; // x.valueOf() is -2, typeof x is a number //x = "5" - 7; // x.valueOf() is -2, typeof x is a number //x = 5 - "x"; // x.valueOf() is NaN, typeof x is a number document.getElementById("demo").innerHTML = x.valueOf() + " " + typeof x; </script> </body> </html>
运行结果