Practice
Resources
Contests
Online IDE
New
Free Mock
Events New Scaler
Practice
Improve your coding skills with our resources
Contests
Compete in popular contests with top coders
Events
Attend free live masterclass hosted by top tech professionals
New
Scaler
Explore Offerings by SCALER

# Comparision Operation & If-Else

This introduces different comparison operators such as ==, !=, >, <, etc that can be used in JavaScript and which data types they can be applied to.

The conditions tested are specified using comparison operators. These operators cause the immediate statement in which they are contained to return a Boolean value of either `true` or `false`.

The following comparison operators are available:

• Equality: `==`, or Inequality: `!=` of any primitive data type (`int`, `char`, `float`, `bool`, etc.) These are binary operators (take two operands) and are specified using `infix` notation (which means that the operator goes in between the two operands).
• Greater-than: `>`, Greater than or equal to: `>=`, Less-than: `<` and Less than or equal to: `<=` are also binary operators using infix notation.
• Negation: `!` is a unary operator, and prefixes the operand.

#### Examples

 Statement Result 10 == 10 true 9 != 6 true !true false 10 > 12 false

Now that you’re familiar with the comparison operators let’s look at the conditional statements.

As the name implies, conditional statements specify whether another statement or block of statements should be executed or not. These are often called “selection constructs”. The two general types are:

• "if…then"
• the "switch…case" construct

### if, if-else and Nested if…else

There are three forms of if…else statements in JavaScript.

1. `if` statement
2. `if...else` statement
3. `if...else if...else` statement

if statement

The syntax of the `if` statement is:

`if (condition) {   // body of if statement}`

The `if` statement evaluates the condition inside the parentheses ( ).

• If the condition evaluates to `true`, the code inside the body of if is executed.
• If the condition evaluates to `false`, the code inside the body of if is skipped.

Example:

`//Suppose num1 = 0 and num2 = 1,if(num1 == 0){    console.log("num1 is equal to 0")}if(num2 == 0){    console.log("num1 is equal to 0")}// Body of first if is executed.// prints num1 is equal to 0`

if…else statement

The `if` statement can have an optional `else` clause. Its syntax is:

`if (condition) {    // block of code if condition is true}else {    // block of code if condition is false}`

If the condition evaluates `true`,

• the code inside the body of if is executed
• the code inside the body of else is skipped from execution

If the condition evaluates `false`,

• the code inside the body of else is executed
• the code inside the body of if is skipped from execution

Example:

`int num = 1;if(num == 0){    console.log("num is equal to 0")}else{    console.log("num is not equal to 0")}// Body of else is executed.// prints num is not equal to 0`

if…else…else if statement

The `if...else` statement is used to execute a block of code among two alternatives. However, if we need to make a choice between more than two alternatives, we use the `if...else if...else` statement.

The syntax of the `if...else if...else` statement is:

`if (condition1) {    // code block 1}else if (condition2){    // code block 2}else {    // code block 3}`

Here,

• If condition1 evaluates to `true`, code block 1 is executed.
• If condition1 evaluates to `false`, then condition2 is evaluated.
• If condition2 is `true`, code block 2 is executed.
• If condition2 is `false`, the code block 3 is executed

Example:

`int num = 0;if(num > 0){    console.log("num is greater than 0");}else if(num < 0){    console.log("num is lesser than 0")}else{   console.log("num is equal to 0")}// Body of else is executed.// prints num is not equal to 0`

Try the following example in the editor below.

Given an integer num denoting the percentage of a student, calculate the grade according to the below rules:

```If num >= 90, grade A.
If num >= 80, grade B.
If num >= 70, grade C.
If num >= 60, grade D.
If num >= 50, grade E.

Print a string consisting of a single character denoting the grade.

Sample Input

`59`

Sample Output

`E`
Hints
• Hints are not available for this problem

## Discussion

Free Mock Assessment
Fill up the details for personalised experience.
All fields are mandatory
Current Employer *
Enter company name *
Select an option *
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
*Enter the expected year of graduation if you're student
Current Employer *
Company Name *