And what exactly is happening here? Well, I have two comparison operators in this console log. When I execute the console log, the first comparison operator executes. This can return a true or false value. And then, the second comparison operator is gonna execute and that is also gonna return a true or false value. When using the "and" operator, if the first condition is true and the second condition is true, the "and" operator will return true.
Now, for these logical operators, we do not have to use comparative operators. We can use direct Boolean values. I'm gonna go ahead and rewrite the console log comparison operators to the value of true and true and I will execute the console log again and there is a value of true to the right. So, the purpose of a logical operator is to find out a true or false condition and based on that true or false condition, make a next step logical decision.
Now I'm gonna replace both true values with false and I'm gonna execute the console again and we see a value of false. So if both conditions are false the "and" operator will always return false. Now I'm gonna replace one of the false values with true execute this to the console again and we see a value of false. So the only time the "and" operator will return true, is if all conditions are true.
Next is the "or" operator, and the "or" operator is represented by two vertical lines also sometimes called "vertical pipes" or "pipe symbol." So I'm gonna go ahead and replace the "and" operator with two vertical pipes. And I'm gonna go ahead and execute this and in the console is a value of true. Now I'm gonna replace the true value in the console log with false and execute this again and now there is a value of false in the console.
So, the "or" operator can be thought of working exactly the opposite of the "and" operator. The "or" operator will return true whenever one of the underlying conditions is true and will only return false if all the conditions are false the last logical operator I'm gonna discuss is the "not" operator.
This operator is represented by an exclamation point. The "not" operator can be applied to any single primitive data type. When used with Boolean values, it will return the opposite, taking true to false and false to true. To demonstrate this, I'm gonna edit the console log by replacing the first false value and the "or" operator with an exclamation point. I'm gonna go ahead and execute this and in the console we see the opposite value of false, true.
Now this is one specific use case. Another use case with the "not" operator is to determine whether a variable has been assigned a value. To demonstrate this, I'm gonna start by deleting the existing console log and I'm gonna declare a variable. Let number. semicolon. I'm not assigning a value. And now I'm gonna console log number. I will execute this and in the console there's a value of undefined. And now I'm gonna take advantage of the "not" operator.
I will go ahead and add the operator to the left of the number variable in the console and execute this again and now in the console is a value of true. When working with either undefined or null, the "not" operator will always return a value of true. Verifying a scenario where you may have a programmatically empty variable or an undefined one. And that's it! Thanks for watching at Cloud Academy.
Farish has worked in the EdTech industry for over six years. He is passionate about teaching valuable coding skills to help individuals and enterprises succeed.
Previously, Farish worked at 2U Inc in two concurrent roles. Farish worked as an adjunct instructor for 2U’s full-stack boot camps at UCLA and UCR. Farish also worked as a curriculum engineer for multiple full-stack boot camp programs. As a curriculum engineer, Farish’s role was to create activities, projects, and lesson plans taught in the boot camps used by over 50 University partners. Along with these duties, Farish also created nearly 80 videos for the full-stack blended online program.
Before 2U, Farish worked at Codecademy for over four years, both as a content creator and part of the curriculum experience team.
Farish is an avid powerlifter, sushi lover, and occasional Funko collector.