Conditions operator and Comparisions operator in javascript

Conditions and Comparision operator in javascript

In javascript conditions and comparision operator are different from each other because of their functions actions and conditions such as.

  • In javascript condition operator ate those operator who perform different role (action) according to their condition.Whereas comparision operator  are those operator which compare their condition with one other to test that whether the condition it's TRUE or FALSE.Comparision operator are also known as logical operator.
  • Condition operator use the statements such asif,  else ,  else if,and  switch. Whereas comparision operator  use ==(equal to),  ====(equal value and equa ltype),  !=(not equal),  !==(not equal value or not equal type),   >(greater than),  <(less than),   >=(greater than equal to),  <=(less than equal to),  &&(and),  ||(or),  !(not).

Condition operator working with if else statement

//Syntax
//if(condition){
//condition1
//} else {
//condition2 
//}

//IF ELSE
const john='student';
if(john==='student'){
     console.log('Status is active');
}else if (john==='teacher'){
     console.log('Status is inactive');
}

By running this code you get  Status is active . keep using else if statement and add the condition but if you want to stop the condition you have to use else at last and give the condition

const john='principal';
if(john==='student'){
     console.log('Status is active');
}else if (john==='teacher'){
     console.log('Status is inactive');
}else{
     console.log('John is not a student neither a teacher');
}

Working with switch statement

//SWITCH
const car='Hyundai';
switch(car){
     case 'Hyundai':
          console.log('This car is Hyundai');
          break;
     case 'Maruti Suzuki':
          console.log('This car is Maruti Suzuki');
          break;
     default:
          console.log('This car is not Maruti Suzuki neither Hyundai');
          break;
}

This work exactly same as if else statement but if you want to use so many condition then switch case is much easier than if else condition.

Comparision operator working with equal (==)

In comparision operator we are working with == this is not just a assignment operator  this(==) indicate the we are comparing two values.In this case it does not matter whether the variable is string or number it is just a number.

//EQUAL TO
const number='400';
if(number==400){
     console.log('This is even number');
}else{
     console.log('This is odd number');
}

Working with equal (!=)

​//NOT EQUAL TO
const number='400';
if(number!=500){
     console.log('This is even number');
}else{
     console.log('This is odd number');
}

Working with equal value and type (===)

​//EQUAL TO VALUE AND TYPE
const number=400;
if(number===400){
     console.log('This is even number');
}else{
     console.log('This is odd number');
}

Working with  not equal value and type (!==)

​//NOT EQUAL TO VALUE AND TYPE
const number='400';
if(number!==500){
     console.log('This is even number');
}else{
     console.log('This is odd number');
}

Working with  Greater or less than (<>)

​//GREATER THAN OR LESS THAN
const number='400';
if(number > 500){
     console.log('This is even number');
}else{
     console.log('This is odd number');
}

Here you get the output This is odd number  but if you use < (less than) in place of  if(number < 500){  you get the output This is even number.

Working with Logical operator (&&, ||, !)

The logical operator is used to test more than one things and resolve logic between value and their variables.

//LOGICAL OPERATOR AND(&&)
const name='john';
const age=22;
if(age > 0&& age < 14){
     console.log(`${name}is a student`);
}else if(age >=15 && age <= 20){
     console.log(`${name}is a teacher`);
}else{
     console.log(`${name}is a staff-member`);
}
//LOGICAL OPERATOR OR(||)
const name='john';
const age=22;
if(age < 16 || age > 24){
     console.log(`${name}is a part of sports team`);
}else{
     console.log(`${name}is a not a part of sports team`);
}

Working with Ternary operator 

In javascript the conditinal operator which is used assign a value to its variable based in aspect of some condition.

​//TERNARY OPERATOR
const number=400;
console.log(number===400?'even':'odd');

 

Keywords: