document.write(4*3); document.write("<<br />>"); document.write("5" + 5); document.write("<<br />>"); document.write("5" - 3); document.write("<<br />>"); document.write(5 * "5");
The output of this example when included in an HTML document is shown here:
Notice in most of the examples the string was converted to a number before calculation and the correct result was produced. Of course, if we would have attempted to do something like "cat" – 3, we would have seen a result of NaN because the string "cat" would convert to NaN and then the subtraction would produce NaN as well. However, in the case of the addition of "5" + 5, the answer was actually the string "55" rather than a number 10. The reason the addition didn’t work is that the plus sign serves two meanings, both as addition and as string concatenation.
var x = "5"; alert (typeof x);
correctly identifies text after x as a string value, as shown here: