Javascript Logical Operators

If you want to test or chain multiple condition, this is a great choice. Testing multiple condition requires multiple chaining and doing it if … else is not efficient.

consider the following example. (&& AND Operator) Allows you to link two or more expressions so that they must all evaluate to true individually for the entire expression to return true.

let age = 20; 
let sex = 'male';
if (age  >= 20 && sex === 'male') {
console.log('You can visit the boys hostel');
} else {
console.log('you are not up to age');

The above example chain two conditions( age and sex). The line with (console.log(‘you visit the boy’s hostel) can only run if both conditions are met, meaning you have to be a male and 20 years plus to enter the boy’s hostel.

consider the following example ( || Or Operator) Allows you to link two or more expressions so that one or more of them must evaluate to true individually before the entire expression can return true

let age = 20; 
let sex = 'male';
if (age  >= 20 II sex === 'male'){
console.log('You can visit the boys hostel');
} else {
console.log('you are not up to age');

From the above example, the line with (console.log(‘you visit the boy’s hostel) can run if one part of the condition is met. Meaning your age can be greater than or equal 20 to visit boys hostel event if are a male or female.

(!) NOT Operator can be used to negate expressions. Let’s combine it with OR in the above example:

if ( !(person || sex === 'male ) ) {
console.log(' Do not open the door'); 
} else {
console.log(' you can open the door');

If the OR statement returns true, the NOT operator will negate it so that the general expression returns false.

You can combine any number of logic statements in any structure.

if ((x === 5 || y > 3 || z <= 10) && (loggedIn || userName === 'simon')) {
  // run the code

In this case, the condition in if (…) will always evaluate to true because 7 (or any other non-zero value) will always evaluate to true.

Switch Statement

In cases where you want to make multiple choices from a selected operations, the switch statement is the best choice. Consider the following example.

switch (expression) {
  case choice1:
    execute the code here

  case choice2:
    execute this of code instead

  // include as many cases as you like

    execute a default line of code if no condition is met.

Switch statement consists of different multiple choices, Keyword switch followed by a set of parentheses. An expression or value in parentheses. The case of a keyword followed by an expression / value selection followed by a colon. Some codes to run if the selection matches an expression A break statement followed by semicolon. If the previous selection matches the expression / value, the browser stops executing the code block at that point and jumps to whatever code appears below the switch statement. In many other cases (steps 3-5) as you wish. Default keyword: after which follows exactly the same code pattern as in one of the cases (steps 3-5), except that default has no choice after it and you don’t have to break the statement because there is nothing to run after that in a block anyway. This is the default option that runs if neither option matches.

Ternary Operator

The ternary or conditional operator is a small piece of syntax that tests a condition and returns one value / expression if it’s true and the other if it’s false – this can be useful in some situations and can take much less code than an if … else block if you have two choices, which are selected by a true / false condition.

( condition ) ? execute this code : execute this code instead

A simple example

let access = ( age >  20 ) ? 'You are an adult' : 'You are a baby'


Fully stack(MEAN) software engineer with over five years of experience participating in the complete product development lifecycle of successfully launched applications. Eager and willing to deliver mission- critical technology solutions.

View all posts by swagasoft →

Leave a Reply

Your email address will not be published. Required fields are marked *