Relational Operators
Tags: javascript, webdev, tutorial, beginners
Published: 2019-12-06 18:58:36

Relational operators are used to compare two variables together and return a boolean result (true or false). They are often used to run conditional elements of code, such as if statements or loops. Most of the relational operators are only applicable to number variable, whereas one of them can be used to compare any data type or any variable.

## Less Than and Less Than or Equal To

The less than operator uses the angle brake '<' symbol. This works the same as in traditional mathematics. We can also add an equals '=' symbol to extend the conditional logic, so it will match numbers that are less than or equal to the given number. Instead of numbers, we can also have given equations, such as two numbers multiplied are less than another two numbers multiplied. The below code snippets show how this works.

The below code will print the letter 'A' as the number 3 is less than the number 5.

```javascript
if( 3 < 5){
	print("A");
}else{
	print("B");
}
```

The below code will print the letter 'B' as the number 9 (3 multiplied by 3) not less than or equal to the number 8 (2 multiplied by 4).

```javascript
if( 3 * 3 <= 2 * 4){
	print("A");
}else{
	print("B");
}
```
## More Than and More Than or Equal To

The more than or equal to operator is similar to the less than operator, but it does the reverse. It uses the opposite angle bracket '>' symbol and can optionally use the equals '=' symbol to extend the conditional logic bounds. A good way to remember the sides of the angle brackets is to remember that the small side is the 'less than' side and the big side is the 'more than' side of the conditional statement. The below code snippets show how this works.

The below code will print the letter 'B' as the number 3 is not more than the number 5.

```javascript
if( 3 > 5){
	print("A");
}else{
	print("B");
}
```

The below code will print the letter 'A' as the number 9 (3 multiplied by 3) is more than the number 8 (2 multiplied by 4).

```javascript
if( 3 * 3 >= 2 * 4){
	print("A");
}else{
	print("B");
}
```

## Double Equals Operators

Double equals operators are when you put two equals '=' symbols next to each other within the code. Within programming, there are single equals and double equals that can be used, and they do different things. A single equals symbol will assign a value to a variable, a double equals symbol checks if one variable is equal to another variable. The double equals relational operator can be used with any data type, not just numbers. The below code snippets explain how these two different equals symbols work.

Within the below code the variable called 'a' is assigned the value of 5. Within the if statement the double equals is used to check if the variable a is equal to the number 5. This means the letter 'A' will be printed and not the letter 'B'.

```javascript
var a = 5;
if(a == 5){
	print ("A");
}else{
	print("B");
}
```

## Relational Variable Initialisation 

Relational operations return a boolean a value, similar to how functions return values. This means we can initialise a variable to the result of a relational operation. As shown below.

Within the below code the variable called 'a' will be assigned the boolean value 'false' as the number 5 is not less than the number 4.
The variable 'b' will be initialised with the boolean value of 'false' also as the string "hello" is not equal to the string "goodbye".

```javascript
var a = (5 < 4);
var b = ("hello" == "goodbye");
```
This article was originally posted on my website: https://acroynon.com/