JavaScript If Else Ternary Statements

JavaScript If Else Ternary Statements

Namaskar🙏🏼

namaskar-namaste.gif (498×498)

Hey everyone, this is Aryan Sharma's blog, and I hope you're all doing well & great.

I'm thrilled to begin this JavaScript blog for absolute beginners.

📍You can find my blogs here↗️

What to wait? Let's go🚀


We often came across the if statement and the conditional operator ?

📌if statement

The if(...) evaluates the condition in parentheses first, and if the result is true, runs a block of code.

Let's see

let age = 10 ;
if( age > 18 ){
    console.log( "can vote" );
}

We recommend using curly brackets around your code block whenever you use an if statement, even if there is just one sentence to run. This increases readability.

Boolean conversion

The if (…) statement evaluates the expression in its parentheses and converts the result to a boolean.

  • A number 0, an empty string "", null, undefined, and NaN all become false. Because of that they are called “falsy” values.

  • Other values become true, so they are called “truthy”.

So, the code under this condition would never execute:

if (0) { // 0 is falsy value
  ...
}

…and inside this condition – it always will:

if (1) { // 1 is truthy value
  ...
}

We can also pass a pre-evaluated boolean value to if, like this:

let cond = (name == "Aryan"); 
if (cond) {
  ...
}

📌else statement

The if the statement may contain an optional else block. It executes when the condition is false.

let age = 10 ;
if( age > 18 ){
    console.log( "can vote" );
}
else{
console.log("Grow child");
}

📌else if statements

Sometimes, we’d like to test several variants of a condition. The else if clause lets us do that.

For example:

let year = prompt('In which year was the ECMAScript-2015 specification published?', '');

if (year < 2015) {
  alert( 'Too early...' );
} else if (year > 2015) {
  alert( 'Too late' );
} else {
  alert( 'Exactly!' );
}

In the code above, JavaScript first checks year < 2015. If that is falsy, it goes to the next condition year > 2015. If that is also falsy, it shows the last alert.

There can be more else if blocks. The final else is optional.

📌Conditional operator ‘?’

Sometimes, we need to assign a variable depending on a condition.

let allowed;
let age = prompt("How old are you?", ' ');

if (age > 18) {
  allowed = true;
} 
else {
  allowed = false;
}
alert(allowed);

The conditional operator allows us to write shorter and in simpler form.

For this example :-

let age = age>18 ? allowed(true) : not allowed(false);

📌Multiple ‘?’

A series of operators with question marks? can return a value that is dependent on more than one criterion.

It is just like a switch or else if condition statements.

let age = prompt('age?', 18);

let message = (age < 3) ? 'Hi, baby!' :
  (age < 18) ? 'Hello!' :
  (age < 100) ? 'Greetings!' :
  'What an unusual age!';

alert( message );

Here the question mark checks if the age is less than 3, it's baby & similarly it checks for another condition after the colon.

if else version👇🏼

if (age < 3) {
  message = 'Hi, baby!';
} else if (age < 18) {
  message = 'Hello!';
} else if (age < 100) {
  message = 'Greetings!';
} else {
  message = 'What an unusual age!';
}

Here I have a task for you!!

TASKS

  • Convert the if statement into '?'

      let height;
    
      if (height < 170) {
        result = '170';
      } else {
        result = 'Less than 170';
      }
    

I Hope you loved the blog❣️

See u in the next blog...Stay tuned🎶

Don't forget to follow me on:
Twitter And LinkedIn