Check if-else logic in javascript syntax

As a programmer, first logical code we write, is “if else” statement, in every programming language we have same logical block, may be slightly different in syntax.

Here we learn how to write if else statement in JavaScript, if-else also known as conditional statement, is one of the very common statement in all programming language, but everywhere there are some difference in syntax.

How to write if else conditional statement in javascript

If else statement can be written in different ways while writing any JavaScript function.

Here are some examples of how to check if else in javascript programming.

  • If condition: it takes Boolean value in parameter, if the value is true, then the logic will go to next step.

    So anything you write any condition, has to return a Boolean value, for example, you can write something like x==10 or x<=10 or x>= 10, you also can compare any string value, like country==”India” etc.

    if(condition)
    {
        // action code
    }
    

  • if else : In addition to above if logic, else block provides a scope to write, if the condition is not satisfied in “if”, then what should happen, like if age is below 18, then show them carton network.

    if(condition)
    {
        // action code 1
    }
    else
    {
        // action code 2
    }
    

  • if else if, here we check two condition if (1) else if (2)

    if(condition 1)
    {
        // action code 1
    }
    else if (condition 2)
    {
        // action code 2
    }
    

  • nested if else in javascript
    nested if or/and if else: nested condition means, if the first if conditions is satisfied, then it goes inside and check another if condition before actually hitting the execution point.
    if(condition)
    {
            // nested if else 
            if(condition)
            {
            }
            else 
            {
            }
    }
    
javascript if else syntax with example

Here we learn JavaScript if else syntax with some example.

so we create a form where we receive user input, if user type India then we display a message saying “You are Indian” else will display “Not sure, if you are Indian!”

Type your country name

Here is the code.
Remember that JavaScript is always case sensitive, so before you compare, you should convert the input into either lower case or upper case using toLowerCase() or toUpperCase() function.

<script>
function checkInput() 
{
    var _input = document.getElementById("txtInput").value;            
if (_input != "" || _input != null) {
    var _formatedInput = _input.toLowerCase();
            
        if (_formatedInput == "india") {
            alert("You are Indian.");
        }
        else {
            alert("Not sure, if you are Indian!");
        }
   }         
}
</script>

Additionally you can also make sure if there is no space in the input string, using trim function you can remove space from both sides.

<script>
var _input = "Hello World   ";
var _inputTrimed = _input.trim();
 
</script>

Sometimes, in real-time programming scenario, you will see “if else statement” like example below, we are checking if value of x is equal to value of y, the assign value 10 to x, else assign value 5 to x, (this is just to make you understand how this one line statement works!

<script>
var x = 2; y = 3;
var result = (x == y ? x = 10 : x = 5);
document.write(result);
</script>

As you can see in above example, if else statement is written in just one single line x == y ? x = 10 : x = 5 ! So, in JavaScript statement we can read like condition? (if true): else;

 
JavaScript programming tutorials to learn JavaScript coding step by step, develop website using JavaScript and html.
Advanced JavaScript
if else statement in JavaScript
javascript Interview Questions Answers
JavaScript Examples | JavaScript Online Course