Logical Operators in JavaScript: A Beginner's Guide


In JavaScript, there are several logical operators that can be used to combine and evaluate multiple expressions. These operators include:

Sno Operator Usage
1. && and
2. || or
3. ! not

The && operator compares two expressions and returns true only if both expressions are true. The || operator compares two expressions and returns true if either or both expressions are true. The ! operator negates the value of a single expression, so if the expression is true, it returns false and if the expression is false, it returns true.

Source Code

HTML Code
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Tutor Joes</title>
</head>
<body>
 
	<script src="js/script.js"></script>
</body>
</html>
To download raw file Click Here

The Below code uses logical operators in JavaScript to perform different comparisons and return a Boolean value (true or false).

The first line of code uses the logical and operator (&&) to check if the value of the variable mark is greater than or equal to 35 and less than or equal to 100. The && operator checks if both conditions are true, and if so, returns true. In this case, since 45 is greater than or equal to 35 and less than or equal to 100, the console will log true.

The second line of code uses the logical or operator (||) to check if the value of the variable a is equal to 2 or equal to 5. The || operator checks if either of the conditions are true, and if so, returns true. In this case, since 5 is equal to 5, the console will log true.

The last line of code uses the logical not operator (!) to check the opposite of the value of the variable a. The ! operator inverts the Boolean value of the variable, so if a is false, the console will log true.

script.js
//Logical Operators in JavaScript 
/*
&&	logical and
||	logical or
!	  logical not
*/
//35-100
let mark=45;
 
console.log(mark>=35 && mark<=100);
 
let a=5;
//2,5
console.log(a==2 || a==5);
 
a=false;
console.log(!a);
 
To download raw file Click Here

List of Programs


JS Practical Questions & Answers


JS Practical Project