Javascript If Statement – Understanding The Concept Of If Statement

What is Javascript if statement?

Javascript if statement is a conditional statement which means executes a code based upon a condition.

For example, you want to write an algorithm for voting for a candidate. Here the condition is, the person’s age should be more than 18.

So, how do you write an algorithm?

Without using conditional statements you can’t write an algorithm for the above condition.

So, here is the example code snippet to solve the above problem.

var age = 19;

if(age > 18 ){

   console.log("vote counted");
}

Ok! let’s know more about javascript if statement.

To write a javascript if statement you need to follow the below syntax.

Syntax :

if(condition) {
       // body of the if condition.
       // executes only when the condition is true
}

In the above syntax, the if is the keyword, and the condition is the javascript expression.

And the curly brackets { } wraps the body of the if statement.

The body of the if statement executes only when the condition evaluates to true.

Consider the following example code snippet to see how if statement works.

var age = 19;

if( age > 18 ){
    console.log("You are eligible to vote ");
}

//expected output is - You are eligible to vote

In the above example, we declare age = 19;

And we are checking the condition age > 18; means executes the if the body, only if the age > 18 evaluates to true.

Here the condition age > 18 evaluates true because the value of age is greater than the 18.

So, the output for the above code is "You are eligible to vote"

Let’s try for another example.

var age = 17;

if( age > 18 ){
    console.log("You are eligible to vote ");
}

//expected no output

In the above example, the value of age is not greater than 18. So the condition evaluates to false. In this case, the javascript interpreter doesn’t print anything.

If you want to execute code even when the condition evaluates to false, you have to use else statement followed by if statement.

else statement

The else statement executes a statement when the if condition evaluates to false.

Syntax :

if(condition){
   //statements to be executed when the condition is true
}else{
   // statementsto be executed when the condition is false
}

Example :

var age = 17;

if(age > 18) {
   console.log("eligible to vote");
}else{
   console.log("not eligible to vote");
}

//expected output is - not eligible to vote

In the above example, we declare variable age to 17. But the condition is, age should be greater than 18. So the condition evaluates to false.

When the condition is false javascript interpreter skips the if block and executes the else block followed by if statement (if provided).

In the above example, the condition (age > 18) is false, so the else block should be executed.

Want to read more about if statement, here is the link to check.

Ok! That’s it for this tutorial In the next article we will learn about javascript else if statement.

If you like this article share with your friends, and if you have any doubts feel free to comment below.

 

Thanks for reading!

Happy Coding!

 

Was this post helpful?
Let us know if you liked the post. That’s the only way we can improve.
Yes0
No0
Powered by Pixelbart

Leave a Reply

Your email address will not be published. Required fields are marked *