The reason is that you are trying to add a number to an undefined
variable, for JavaScript undefined + 10
for ex returns NaN
, you could try it out with the following:
var cartTotal;
cartTotal += 10;
console.log(cartTotal);
For a great explanation on the difference between null
and undefined
look at this answer: Why is null an object and what's the difference between null and undefined?
Loosely typed means that a variable is declared without its type, so
var answer = 42;
is loosely typed, it's just a generic box holding the Number
42, when it comes to number addition the compiler knows how to sum the two and gives you the result.