Logical-Comparison-Operators in Javascript

aryakris

Arya Krishna

Posted on May 7, 2022

Logical-Comparison-Operators in Javascript

Let's see the operators in Javascript.

var a = 10
var b = 5
Enter fullscreen mode Exit fullscreen mode

Here if we can use the Arithmetic Operators. What does Arithmetic Operator do? Arithmetic operators combine with numbers to form an expression that returns a single number.

console.log(a + b);
console.log(a - b);
console.log(a / b);
console.log(a * b);
Enter fullscreen mode Exit fullscreen mode

Here each of the console.log will give the respective answers - 15, 5, 2, 50.

Similar to the Arithmetic operators we have the modulus. Modulus returns the remainder between two numbers.

console.log (a%b)
Enter fullscreen mode Exit fullscreen mode

There are also comparison operators. There are problems in which we need to make decisions in coding. All decisions really comes down to making a comparison. Two values comparing to each other to ultimately determine a true or false result. Comparison operators combine with strings, booleans and numbers to form an expression that evaluates to true or false.

Beginners often gets confused when asked the difference between == and ===
Here == compares equality.
For example console.log(b == c); where b ="50" and c =50. Here both the values are 50 while one is a string data type and other is a number. Here in this example b==c is true while b===c is false. That is because ===Compares equality and type (strict equality).

Other comparison operators are -

  1. Greater than or less than
  2. Greater than or equal to and less than or equal to
var a = 100;
var b = 10;
var c = "10";
var expression1 = (b == c);
var expression2 = (a > b);
Enter fullscreen mode Exit fullscreen mode
  1. && - When we do console.log(expression1 && expression2);, evaluates to true if expression1 AND expression2 are both true, otherwise false
  2. || - Similarly if we use || operator, evaluates to true if expression1 OR expression2 is true, otherwise false
💖 💪 🙅 🚩
aryakris
Arya Krishna

Posted on May 7, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Mastering the For Loop in JavaScript
javascript Mastering the For Loop in JavaScript

September 22, 2023

JS Test #3: try/catch
javascript JS Test #3: try/catch

December 18, 2022

using pseudocode as a beginner - why and how
javascript using pseudocode as a beginner - why and how

September 14, 2022

Method chaining in Javascript
javascript Method chaining in Javascript

September 13, 2022

Demystifying Fetch()
javascript Demystifying Fetch()

July 12, 2022