&& and ||
In JavaScript, we have the three (really four, but I’m omitting ??) logical operators seen above, &&, !, and ||. These operators can work for any value, boolean (true/false) or otherwise. These operators evaluate an expression, converts the operand to a boolean value, and, according to the rules of each operator, may or may not return the value of the operand. For the || operator, the OR operator, the first true value will be returned. This means that only one OR the other value must convert to true for the value of the operand to return. Practically, as we see this operator used, if you have an if statement and only one condition evaluates to true, the code will still run. Conversely, for the && operator, the AND operator, both statements must convert to true for the code to run. The ! operator, or NOT operator, doesn’t work similarly to the first two – ! will return the inverse boolean value of an operand. The explanations of these operators don’t seem to make much sense when written out, but they are fairly intuitive when you are actually coding. For now, I try to just remember them as AND and OR, and not confuse myself with the intricacies.
Leave a Reply
You must be logged in to post a comment.