PowerShell left hand rule
PowerShell uses the “left hand” rule when deciding how to interpret operators. Meaning that the type of the value on the left side of the operator is dominant. Here are some examples:
2+2 (gives 4)
2+”2″ (gives 4)
“2” + 2 (gives “22”, string data type wins out using + for concatenation)
“2” + 5 (gives “25”, concatenation again)
To have a little more fun try some multiplication:
3 * 3 (gives 9, ok this is still boring)
4 * “5” (gives 20, the “5” becomes an integer)
“3” * 4 (gives “3333”, that’s more interesting, “3” means the string data type is the primary type, this acts much like Ruby)
Here is a bit more that might be interesting:
15,30,45 + 3 (gives a new list of 15,30,45,5)
15,30,45 * 3 (gives a new list of 15,30,45,15,30,45,15,30,45)
Kind of cool how this works. The DOS prompt was stone-age comparatively.
That was informative, thanks. One small correction though: 15,30,45 + 3 (gives a new list of 15,30,45,3)
Adam Houston
September 24, 2014 at 12:40 pm