Why is it that in JavaScript I can perform operations such as multiplication and subtraction with strings that are digits e.g. "10" with numbers?
Does JavaScript do type inference?
Consider the example below, why is it in the last two statements I get 1010 and not 20 in either one?
var foo = "Hello, world!";
var bar = "10";
var x = foo * 10; // x is now bound to type number
console.log("type of x= " + typeof x + ", value of x= " + x); // this will print number NaN, that makes sense..
var y = bar * 10; // y is now bound to type number
console.log("type of y= " + typeof y + ", value of y= " + y); // this will print number 100
y = bar - 10; // y is now bound to type number
console.log("type of y= " + typeof y + ", value of y= " + y); // this will print number 0
y = bar + 10; // y is now bound to type string!!
console.log("type of y= " + typeof y + ", value of y= " + y); // this will print number 1010
y = eval(bar + 10); // y is now bound to type number!!!!
console.log("type of y= " + typeof y + ", value of y= " + y); // this will print number 1010
output from the log:
type of x= number, value of x= NaN
type of y= number, value of y= 100
type of y= number, value of y= 0
type of y= string, value of y= 1010
type of y= number, value of y= 1010