var x = “Hello World” + 14 + 6;
console.log(x)
What do you expect to see now that we know how JavaScript evaluates expressions? If you’ve been paying attention, you will have guessed that the output would be ‘Hello World146’ and you would be absolutely right. Let’s step through it again. JavaScript sees the ‘Hello World’ and decides it is a string, then it sees the number 14, decides it can’t add a string and a number together, so it must join them instead, at which point we have the string ‘Hello World14’; then it encounters the final part of the expression, 6, at which point JavaScript understands that it can’t add together the string ‘Hello World14’ with 6, so it must also join them together. Resulting in an output of ‘Hello World 146’.
In JavaScript data types are dynamic. This means that we can swap out variables to contain different data types at will. If we have a variable that contains a number, we can very easily convert that to a string and vice versa. This is incredibly useful because it means we are never restricted to what we can assign to our variables, even if they have already been declared. The following example highlights this benefit.