JavaScript variables can hold many data types, such as numbers, strings, arrays, objects, booleans, and more:
In programming, data types is an important concept. To be able to operate on variables, it is important to know something about the type. Without data types, a computer cannot safely solve this:
var x = 16 + "Volvo";
Does it make any sense to add "Volvo" to sixteen? Will it produce an
error or will it produce a result? JavaScript will treat the example above as:
var x = "16" + "Volvo";
When adding a number and a string, JavaScript will treat the number as a string. Therefore, the value of x is 16Volvo. Note: In the case of var x = "Volvo" + 16;, the value is Volvo16.
JavaScript evaluates expressions from left to right. Different sequences can produce different results:
var x = 16 + 4 + "Volvo"; (The result: 20Volvo)
var x = "Volvo" + 16 + 4; (The result: Volvo164)
In the first example, JavaScript treats 16 and 4 as numbers, until it reaches "Volvo". In the second example, since the first operand is a string, all operands are treated as strings.