If Statement In JavaScript With Examples

If Statement In JavaScript With Examples

SURYA L's photo
SURYA L
·May 26, 2022·

1 min read

Subscribe to my newsletter and never miss my upcoming articles

Play this article

Table of contents

  • What is IF Statement:

Hello everyone hope everyone is doing well. My name is Surya L, the purpose of this blog is to teach all about If Statement In JavaScript With Examples.

What is IF Statement:

If a condition is true, the if statement executes the block. The syntax of the if statement is as follows:

if( condition )
   statement;

An expression or a value can be used as the condition. The condition is usually evaluated to a Boolean value, which is true or false.

The if statement executes the statement if the condition is true. In any other case, the if statement passes control to the next statement after it.

Flowchart of IF Statement:

if-block.png

function proj(n)
{
    if(n>0)
    {
        console.log("Its Positive");
    }
}
proj(2)

The Output of above code is 👇 Its Positive

For example if the n value is -2

function proj(n)
{
    if(n>0)
    {
        console.log("Its Positive");
    }
}
proj(-2)

There will be no output

Conclusion:

  • If a condition is true, the if statement executes the block.
  • In any other case, the if statement passes control to the next statement after it.

Credits: I learned this topics in FreeCodeCamp which I explained in minified version

Thanks for reading the blog. Do let me know what you think about it.

 
Share this