If Statement In JavaScript With Examples

If Statement In JavaScript With Examples

ยท

1 min read

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.

ย