TCSL0ZGITJ Values used in JavaScript multiplication are converted to numbers with the Number() casting function before they are used in the operation. Empty strings will become 0 and Boolean true will become 1.

Addition on strings concatenates the two strings. If one of the operators is a string and the other is not, the non-string is converted to a string and the two are concatenated.

var x = 10 + 10;   //numbers - 20
var y = 10 + "10"; //strings = "1010"
var x = 5;
var y = 20;
var z = "5 + 20 = " + x + y;    // converts to a string since operators work in order  "5 + 20 = 520"
var z = "5 + 20 = " + (x + y);  // converts the (x + y) first  "5 + 20 = 25" 

Logical operators work as expected in Math until you start comparing strings. The numerical character values are compared, so uppercase letters come BEFORE lowercase letters. Numbers don’t compare as expected if they are strings either.

var x = "A" < "a";    // true
var x = "A".toLowerCase() < "a".toLowerCase();  //false
var x = "44" < "5";  // true    both strings, not converted, character values are used
var x = "44" < 5;    // false   converted to numbers
var x = "blue" < 5;  // false   blue is converted to NaN

Leave a Comment

Your email address will not be published.

Scroll to Top