When writing JavaScript I always took for granted that adding two integers together resulted in another integer. Or that adding two strings together would result in the concatenation. But I got to thinking, how does the language determine the types of instances behind the scenes prior to performing operations using those instances?
var one = 1;
var two = 2;
var fourStr = 'four';
var floaty = 1.5;
//this results in an integer
var three = one + two; //3
//but this results in a string
var result = fourStr + one; //'four1'
//and this results in a float
var floatenized = one + floaty; //2.5
Does the runtime just determine the instance types and then reference some sort of internal type precedence or something? Can anyone explain exactly how these operations are performed by the runtime when instances of differing types are combined?