Open In App

Logical Operators in math.js

Last Updated : 06 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In Math.js, we can use logical operators to perform boolean operations on values. The math.and function perform a logical AND, math.or handles logical OR, and math.not provides logical NOT. These operators allow us to evaluate boolean expressions and handle logical conditions effectively in mathematical computations.

These are the following Logical Operators that we are going to discuss:

Logical AND (&&)

The Logical AND can be used in Math.js with the math. and function to determine if both operands are true. It returns true only if both values are true, otherwise it returns false. For example, math.and(true, true) results in true, while math.and(true, false) and math.and(false, false) both yield false.

Syntax:

math.evaluate('A && B')

Example: The below example uses Logical AND (&&) in math.js.

JavaScript
const math = require('mathjs');

const result1 = math.and(true, true);   
const result2 = math.and(true, false);  
const result3 = math.and(false, false); 

console.log(result1); 
console.log(result2); 
console.log(result3); 

Output:

true
false
false

Logical OR (||)

The Logical OR can be used in Math.js with the math.or function to determine if at least one of the operands is true. It returns true if at least one value is true, otherwise it returns false. For example, math.or(true, true) and math.or(true, false) both result in true, while math.or(false, false) yields false.

Syntax:

math.evaluate('A || B')

Example: The below example uses Logical OR (||) in math.js.

JavaScript
const math = require('mathjs');

const result1 = math.or(true, true);   
const result2 = math.or(true, false);  
const result3 = math.or(false, false); 

console.log(result1); 
console.log(result2); 
console.log(result3); 

Output:

true
true
false

Logical NOT (!)

The Logical NOT can be used in Math.js with the math.not function to negate a boolean value. It returns true if the input is false, and false if the input is true. For example, math.not(true) results in false, while math.not(false) results in true.

Syntax:

math.evaluate('!A')

Example: The below example uses Logical NOT (!) in math.js.

JavaScript
const math = require('mathjs');

const result1 = math.not(true);  
const result2 = math.not(false); 

console.log(result1); 
console.log(result2); 

Output:

false
true

Logical XOR (⊕)

Logical 'xor' operator returns true if exactly one of the operands is true; otherwise, it returns false.

Syntax:

math.xor(a, b)

Example: The below example uses Logical XOR (⊕) in math.js

JavaScript
const math = require('mathjs');

const result1 = math.xor(true, true);   
const result2 = math.xor(true, false);  
const result3 = math.xor(false, false);

console.log(result1); 
console.log(result2); 
console.log(result3);

Output:

false
true
false

Combining Logical Operators

You can combine logical operators such as AND (and), OR (or), NOT (not), and XOR (xor).

Example: The below example combines different logical operators in math.js

JavaScript
const math = require('mathjs');

const a = true;
const b = false;
const c = true;

const result1 = math.and(a, b);  
const result2 = math.or(a, b);   
const result3 = math.not(b);   
const result4 = math.xor(a, c);  

// Combining operators
const combinedResult1 = math.and(math.or(a, b), math.not(c));  
const combinedResult2 = math.xor(math.and(a, b), math.or(a, c)); 

console.log(result1);
console.log(result2);
console.log(result3);
console.log(result4);
console.log(combinedResult1);
console.log(combinedResult2);

Output:

false
true
true
false
false
true

Next Article

Similar Reads