In this post we will go over the following concepts in JavaScript
– Operators
– Operator Precedence
– Operator Associativity
– Coercion
Operator Basics
We are taught from a young age that a plus sign between two numbers results in the addition of them and one number returned. To keep this same syntax in code rather than something like add(x,y), we have operators in JavaScript. Operators are special functions that are invoked differently syntactically than regular functions.
result = 2 + 2 - 5;
We have some operators in the above code block, the assignment(=) operator, the addition(+) operator and the subtraction(-) operator.
– The assignment operator will assign a value to it’s left operand based on what is it’s right operand.
– Ignoring primitive type coercion now which we’ll get to later in this post, the addition operator will return the sum of the left and right numeric operand.
– The subtraction operator will return the difference between the left and the right numerical operand.
Now that we know what these operators do I’d like to walk you through how the JavaScript engine will interpret the code block. However, we are still unable to fully grasp what is going on with just this knowledge. We’re missing two concepts that are essential to understanding what is going on, operator precedence and operator associativity.
Operator Precedence is a value that is assigned to each operator which helps it determine which operator gets to execute first. Operator Associativity is what order the operator gets called in when they have the same precedence. Addition and Subtraction both have a precedence of 14 and are left to right associative. Assignment has a precedence of 3 and is right to left associative.
The JavaScript engine will interpret (result = 2 + 2 – 5) as follows:
The engine compares the operator associativity of the operators and finds that addition and subtraction are higher so they will be executed first. Since addition and subtraction are left to right associative, addition will be executed first with the values 2 and 2 leaving us with the following expression
result = 4 – 5
Then the engine will invoke the subtraction operator
result = -1
Finally the engine will assign the value of -1 to the spot in memory that result is pointing to and return -1 to from the assignment which is thrown away as nothing will be using this value after this is over.
var = var2 = 4
Assignment returns the value of what was assigned. This allows us to write some concise code like above. Here the second assignment is invoked first which return a value of 4 after assigning it to var2. Then the first written assignment operator is invoked and var is assigned the value of 4.
Comparison Operators and Variable Coercion
if (0 == null) {
console.log("JavaScript thinks null is the same as zero.")
}
if (0 === null) {
console.log("JavaScript thinks null is the same as zero.")
}
How many times will the phrase, “JavaScript thinks null is the same as zero.”, be logged to the console? If you’ve been working with JavaScript in the past, you may know that the phrase will only be logged once. The two if statements are identical except they use different operators. The equality operator(==) and the strict equality operator(===) differ in one key way. The equality operator will attempt to coerce both variables to be the same type before checking for equality contrary to the strict operator. The equality operator will coerce the value null to 0 and the expression will return true. The strict operator will not do so and return false.
JavaScript having these properties can lead to very powerful intuitive code when used properly and can also result in painful debug sessions when not used without thought. Here is one example where coercion can be very helpful.
// unknownParameter is the result of an api call, it will return a string if successful or an empty string if not successful
let unknownParameter = api("get")
let parameter = unknownParameter || "default"
JavaScript’s coercion property allows us to write some powerful, concise and readable code. The logical or(||) operator has a higher precedence than the assignment operator and will execute before it. If the api call failed and the unknownParameter’s value is an empty string, JavaScript will coerce the empty string to false and move on to the right operand “default”. Any string other than an empty string coerces down to true. JavaScript engine will then take this value that coerced to true and return the uncoerced variable which is assigned to parameter. If the api call had succeeded, unknownParameter would have coerced to true and been returned from the logical or operator. This is some heavy lifting that was done in only a few characters, powerful stuff!
I hope you have a better understanding of what’s happening behind the scenes when it comes to JavaScript’s use of operators. If I’ve made any errors or you have any questions please feel free to post any comments!
Bonus: Here is a helpful link for operator precedence if you’d like to explore!
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence






