Logical Operators in javascript

Logical Operators in javascript

Logical operators in JavaScript programming are used to execute multiple JavaScript programs by combining custom conditions or expressions. In most JavaScript programs, multiple conditions or expressions can be executed with control flow statements (e.g., if statements) in particular code or statements. Logical operators return a boolean value (true or false) as a result based on the evolution of the given condition. Mainly, there are three types of logical operators in JavaScript language. e.g., AND (&&), OR (||), and NOT (!) operators.

Logical Operators in javascript

So, let’s know each logical operator in JavaScript programming.

Logical AND (&&) operator.

Logical AND (&&) operator in JavaScript programs returns true value in the given program condition. Where both program logic operands (conditions) are true, or false otherwise.

Syntax of Logical AND (&&) operator.

condition1 && condition2

Example of Logical AND (&&) operator.

let p = 3;

let q = 7;

console.log(p > 1 && q < 10); // result – true (here both AND operator conditions are true)

console.log(p > 3 && q > 10); // result – false (here second AND operator condition is false)

Here in AND operator if both the conditions are true then the output result is true.

Here if any of the two conditions in the current condition is false then the result is false.

&& operator short-circuiting in JavaScript.

&& operator short-circuiting in JavaScript program. If the evolution of the first condition in the given programming condition is false, then JavaScript will not evolve to the second condition in the program because the complete result in the current logic flow testing will already be false.

let p = false;

let q = 7;

console.log(p && q); // Result – False (Here this condition short-circuits and does not test q)

Logical OR (||) operator.

Logical OR (||) operator in JavaScript programming returns true value if at least one of the two logical operands (conditions) in a logic is true. Logical operator returns false value in JavaScript program only when both the operands in a logic are false at the same time.

Syntax of Logical OR (||) operator.

condition1 || condition2

Example of Logical OR (||) operator.

let p = 7;

let q = 9;

console.log(p > 4 || q < 17); // Result – True (Here in and operator first condition is true)

console.log(p < 5 || q > 14); // Result – False (Here in and operator both conditions are false)

Always remember if any condition in and operator is true then result is true.

Here if both the given conditions are false then result is false.

JavaScript or Operator Short-circuiting.

In JavaScript programming || operator evolves to short-circuit. Where if first condition is true then JavaScript program will not evolve to second condition because complete result will already be true.

let p = true;

let q = 4;

console.log(p || q); // Result – true (here short-circuit in or operator and does not test q)

Logical NOT (!) operator.

The logical NOT (!) operator in JavaScript is used to avoid or reverse the true value of a condition in the current program. Where it returns true value if the current condition is false, and if the given program condition is true, it returns false value.

Syntax of logical NOT (!) operator.

!condition

Example of logical NOT (!) operator.

let p = true;

let q = false;

console.log(!p); // Result – false (here p variable value is true, so !p result is false)

console.log(!q); // Result – True (Here q value is false, so !q result value is true)

Negation operator – In any JavaScript program, if the given condition is true, then it becomes false. If the given condition is false, then it becomes true.

Combination of logical operators in JavaScript program.

You can use all logical operators to create complex conditions in JavaScript program based on your program development need.

Example of Combination of logical operators.

let p = 7;

let q = 11;

let r = 17;

console.log(p > 4 && (q < 18 || c === 17)); // Result – True (Here both the conditions are true)

console.log(!(p > 4) || (q > 13)); // Result – False (Here first condition is false, similarly second condition is also false)

True and False Values ​​​​in JavaScript.

In JavaScript programming some values ​​​​are considered as falsy, which means they are treated as false when evolved in logical context. All other values ​​​​are treated as true, which means they are treated as true.

Example of False values ​​​​in JavaScript.

let info = 0;

let info1 = “vcanhelpsu”;

console.log(info && info1); // Result – 0 (Here value is false)

console.log(info1 && info); // Result – 0 (because info1 is false)

console.log(info || info1); // Result – “vcanhelpsu” (because 0 is false, so it returns true value)

JavaScript Summary of Logical Operators.

OperatorDescriptionExampleResult
&&JavaScript Logical AND operator (it returns true when if both conditions are true)true && falsefalse
||JavaScript logical or operator (it returns true when one condition true, it return false when both condition is false at same time)||Logical OR (returns true if at least one condition is true)
!Logical NOT operator (it use to negates the truth value)!truefalse

Some examples of logical operators in JavaScript.

Example of logical operators.

Here you have to check if a user is logged in, and has privilege to access a webpage or database.

let isLogIn = true;

let userPermison = false; // result – User Access denied (because userPermison is false)

if (isLogIn && userPermison) {

console.log(“User Access granted”);

} else {

console.log(“User Access denied”);

}

Example of checking valid user session in JavaScript.

let clientSession = null; // result – client Session expired (because clientSession is set false)

if (!clientSession) {

console.log(“Client Session expired”);

}

Example of validating input field in JavaScript.

let input = “Vcanhelpsu”;

if (input && input.length > 1) {

console.log(“Valid user text input”);

} else {

console.log(“Invalid user text input”);

}

// result is – Valid user text input (hare the input is true and text length is greater than 1)

Logical Operator Conclusion in JavaScript Programming.

Logical operators in JavaScript programs are important to control the flow of a program based on multiple program conditions. Logical operators allow you to combine AND, OR, and NOT based on your programming development needs, so that decisions can be made more efficiently in the existing program.