DEV Community 👩‍💻👨‍💻

Cover image for Logical-Comparison-Operators in Javascript
Arya Krishna
Arya Krishna

Posted on

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

Top comments (0)

🌚 Browsing with dark mode makes you a better developer by a factor of exactly 40.

It's a scientific fact.