Use this article as a reference sheet for JavaScript comparison and logical operators. Show
Comparison OperatorsYou may be familiar with comparison operators from math class. Let’s make sure there aren’t any gaps in your knowledge.
Logical OperatorsComparison operators allow us to assert the equality of a statement with JavaScript. For example, we can assert whether two values or expressions are equal with There are scenarios, however, in which we must assert whether multiple values or expressions are true. In JavaScript, we can use logical operators to make these assertions.
The Truth Values in a computer : BooleansAt the end of the day, one of the few things, and most powerful things a computer can determine if a statement (expression) is "true" or "false". Because true and false are so prevalent in decision making, they are their own keywords. In other words, in a program we write true not the string 'true'. Logical Operators: AND, OR, and NOTWe can combine several "boolean" statements that have true/false meaning into a single statement using the key concepts AND and OR (and NOT). If I want to go to the movie AND I have enough money, then I will go to the movie. BOTH conditions have to evaluate to true (have to be true) before the entire expression is true. OR is written with double "pipes" || AND is written with double "ampersands" && True is written: true; False is written: false; Not is written in a variety of ways. In Matlab it is the tilde (~). In C, Java, ActionScript, it is written as the exclamation point (!). Warning: Again, the two Booleans are true and false, (not the strings 'true' and 'false' but keywords true and false. Further, while Matlab allows you to use 1 and 0 for true and false, every time you write a program and need to assign the value's true or false, you should use the keywords true and false, not the shortcut 1,0. Truth Tables, Logic, and DeMorgan's LawsComputer programs are constantly making decisions based on the current "STATE" of the data held by the program. For example, a heart monitoring program might sound an alarm if the pulse is too slow or the blood pressure is too weak. Combining multiple conditions to form one True/False value is the domain of Logic. The primary way to combine two boolean expressions into one is through the use of AND or OR. In most programming languages, AND is written using double ampersands: &&. OR is written using double pipes: || Truth tables show the result of combining any two expression boolean expressions using the AND operator and the OR operator (or the NOT operator). You should memorize/learn these values and be able to duplicate this table:
DeMorgan's LawsDeMorgan's laws tell us how to transform logical expressions (with multiple AND and/or OR statements) using the NOT operator. For example: You want your loop to finish after 8 iterations have been made (and you want to use a while loop). This is the same as saying: You want your loop to continue when less than or equal to 8 iterations have been made. Thus to continue, you want the opposite (or the NOT) condition of the finishing condition. The pseudocode looks something like: which becomes: Now say their is an additional criteria to finishing, say when the total value of some variable is greater than 100. How you would say this in English: which becomes: DeMorgan's Laws tell us how to translate from a "positive" version of a statement into a negative version, or more concisely: The DeMorgan laws tells us how to translate an logical expression ( E ) into NOT( E ). Here is what DeMorgan's law states that: NOT(A || B) is Equivalent to (NOT(A) AND NOT(B)) Additionally: NOT(A && B) is Equivalent to (NOT(A) OR NOT(B)) This goes in reverse as well: (NOT(A) || NOT(B)) is Equivalent to NOT(A AND B) Thus if you are having problems thinking of when something should continue, think about when it should stop and then use NOT! Back to Topics List Which operators are used in logic?Common logical operators include AND, OR, and NOT.
What are the 3 logic operators?The three basic boolean operators are: AND, OR, and NOT.
Which operators give variables their values?Assignment operators assign values to variables. Logical operators compare two values and, based on whether the comparison is true (or false), return either a “true” or “false.”
What are the 4 logical operators?There are four logical operators in JavaScript: || (OR), && (AND), ! (NOT), ?? (Nullish Coalescing).
|