Conditional Statements in JavaScript


Published on 05 April 2022


Conditional Statements in JavaScript

Conditional Statements in JavaScript to improve its structure

In javascript the following conditionals can be used within any code depending on any option that we must determine:

  • if, else if, ... If a condition is met then we show one part of the code, otherwise we show another.

Example:
Imagine the case that we want to buy a hotel night, whose price is €30, and we have €50. 

If our amount of money is greater than €30 we can buy the hotel night, otherwise we will not be able to do so.

 const mymoney = 50;
 if(mymoney > 30){
  console.log('Hotel night purchased');
 } else if(mymoney == 30){
  console.log('Hotel night purchased');
 } else {
  console.log('It has not been possible to buy the hotel night');
 }

This example can also be written in JavaScript by using the Ternary Operator:

const mymoney = 50;
 (mymoney > 30) ? console.log('Hotel night purchased') : console.log('It has not been possible to buy the hotel night');


Previous Chapter - Next Chapter



Tips on SEO and Online Business

Next Articles

Previous Articles