Like other programming languages, JavaScript also comes with rich content of comparison operators in JavaScript. There are multiple types of comparison operators in JavaScript. JavaScript is one of the most popular high-level programming languages and is used in client-side programming and is generally known as a client-side scripting language.

What are comparison operators?

In JavaScript programming language comparison operators are known for comparing two values. It can either be String or integers. Comparison operators are on of the most powerful built-in function in JavaScript language. Comparison operators actually compares two values with each other. It compares the value on left to the value on the right side.

There are generally 5 types of comparison operator in JavaScript.

  • Equals to (==)
  • Less than (<)
  • Greater than (>)
  • Less then or equals to (<=)
  • Greater than or equals to (>=)

Equals to Operator

Equals to operator is used to compare two values either the two values are equal to each other or not. Equals operator allows us to execute the block of code specified under the equals to operator.

For example, we need to compare two integers to check whether these values are equal or not. If the two integers are equal to each other. If the two values are equal to each other then the below code will be executed.

Code

let x=10

let y=10

if(x==y)

{

console.log(“Values are equal”)

}

else

{

concole.log(“Values are not equal”)

}

Output

Values are equal

Less than Operator

Less than operator compares the two value to check if the value on the left side is equal to the value on the right side.

Code

let x=10

let y=8

if(x<y)

{

console.log(“Values are equal”)

}

else

{

concole.log(“Values are not equal”)

}

Output

Values are not equal

Greater than Operator

In JavaScript programming language, Greater than operator is used to comapre if the value on the left side is greater than the value on the right side.

Code

let x=10

let y=8

if(x>y)

{

console.log(“Values are equal”)

}

else

{

concole.log(“Values are not equal”)

}

Output

Values are not equal

Less than or Equals to Comparison Operator

In JavaScript programming language, Less than or equals to operator is another most powerful operator used in JavaScript programming language. Less than or equal to comparison operator in JavaScript programming language compares the value on left side to the value on the right side. It works in multi directions. It checks if the value on the left is less than or even if the value or the left side is equal to the value on the right side then it allows the block of code to execute.

What is the difference between Equals to operator and Less than or equals to operator in Comparison Operators in JavaScript?

In JavaScript programming language, there is a huge difference between these two comparison operators. Equals to operator checks only if the two values are equal to each other otherwise the block of code in curly brackets under equals to operator is not executed. Similarly if the values are only equal only then the code executes.

Whereas, in Less than or equals to operator, the code is executed in such an extent that it checks if the value on the left side is lesser or equal to the value on the right side. The block of the code will not be executed if the value on the left side is greater than the value on the right side.

Code

let x=7

let y=10

if(x<=y)

{

console.log(“X is smaller than Y”)

}

else

{

concole.log(“X is not smaller than Y”)

}

Output

X is smaller than Y

Greater than or Equals to Assignment Operators

Same as less than or equals to operator, greater than or equals to operator works in the same way but it checks the values in either ways. it checks the values if the values on the left side is greater than the value on the right side or if the value on the left side is greater than the value on the right side.

Code

let x=10

let y=10

if(x>=y)

{

console.log(“True”)

}

else

{

concole.log(“False”)

}

Output

True

RECOMMENDED FOR YOU BY SHOPPYSPIDER

Leave a Reply

Your email address will not be published. Required fields are marked *