logical or js code example

Example 1: js logical operators

Javascript Logical Operators
&& Logical and
|| Logical or
! Logical not

Example 2: or operator javascript

var a = 2;
var b = 5;
var c = 10;

if (a === 3 || a === 2) {
	console.log("TRUE");
} else {console.log("FALSE");}
if (a === 4 || b === 3 || c === 11) {
	console.log("TRUE");
} else {console.log("FALSE");}
if (b === 5 || c != 10) {
	console.log("TRUE");
} else {console.log("FALSE");}

/* Output:
TRUE
FALSE
TRUE
*/

Example 3: and operator in javascript

//&& returns true if both values are true
//or returns the second argument if the first is true
var a = true
var b = ""
var c = 1

true && "" //""
"" && 1 //""
false && 5 //false

Example 4: and operator in javascript

//& (bitwise AND) operator
console.log(5 & 13); //outout: 5
/*
5  = 0101 (base 2)
13 = 1101 (base 2)
//AND every bit together from both numbers
+----+
|0101|
|1101|
+----+
|0101|
+----+
0101 = 5 (base 10)
*/

Example 5: logical operators javascript

// There are 3 Javascript Logical Operators
// || (OR)
// && (AND)
// ! (NOT)

if (a || b) {
	console.log("I will run if either a or b are true");
}

if (a && b) {
	console.log("I will run, if and only if a and b are both true");
}

if (!a) {
	console.log("I will only run if a is false");
}

if (a) {
	console.log("I will only run if a is true");
}

Tags:

Misc Example