programminghead Logo

JavaScript Tutorial 6 : JavaScript Conditional Statements

JavaScript Conditional Statements :

Conditions Statements are used to perform different actions according to given condition. For example : i want to buy a Product called "Samsung USB Flash Drive" and there is two available shops near my House who sells products like USB Drives. So i went to the First shop but i can't find my product called "Samsung USB Flash Drive" in the First shop then i have to go to the 2nd shop to buy that product.
so you can take JavaScript Conditional as my previous Example, where 2 available shops are the two different Conditions and product is the Condition, so if my product is available or my condition is true at first Statement then i can skip 2nd shop or 2nd statement, but if my product or my condition is false at first shop or 1st statement the i have to go for 2nd shop or 2nd statement.

JavaScript Conditions :

In JavaScript we have two Different Conditions :-
1. If Condition
2. Else Condition.

The if Statement (if(){....}) :-

In the IF statement we provide Condition inside IF's parentheses (condition) and JavaScript Program execution will depend on the given Condition.
if the given Condition return true then code inside the IF's Braces {code} will be executed or if Condition return false then JS program will ship the IF Statement's part/Code.

IF Statement Syntax:


   if(condition){
     JS code...
   }

IF Statement Example:


<html>
 <head>
  <title> IF statement </title>
 </head>
 <body>

   <script type="text/javascript">

    var a=10;
     
    if(a==10) {
      document.write("a is = 10");
    }

   </script>
 </body>
</html>

Result


single = vs double == :

single = assigns value to data Types like a=10;
Double == used to describe that data is equal like a==10 thats mean a is equal to 10 but this is not assigning any value to the Variable a. if variable is equal to 5 then a==10 condition will be false, but if variable a value is 10 and we are using single a=5 then it will reassign value to our variable.

Else Statement :

Else Statement specify another or alternative code to be Execute when IF Condition is false.
Normally JS Program checks IF condition true or false, if condition is True the program will Execute IF Statement's Data or IF the IF Statement's condition is false then Program will skip the IF Statement's code.
But Else statement specify another or alternative code when IF Statement condition is false.
Else Statement Executes only when the IF Statement is False that's why we don't have to gave condition in else Statement.

Else statement Syntax :


   else{
     JS Code....
   }

Else statement Example :


<html>
 <head>
  <title> Else statement </title>
 </head>
 <body>

   <script type="text/javascript">

    var a=5;
     
    if(a==10) {
      document.write("a is = 10");
    }
    else{
      document.write("a is not = 10");
    }

   </script>
 </body>
</html>

Results :


else if Statement : ( else if(){...} ) :

If we use only IF and Else statement inside out JS Program, then out program will only take one condition at a time, but using else if statement we can add multiple conditions in JS Program.

else if Syntax


    if(condition 1){
     js Code..
    } 

    else if(condition 2){
     js Code..
    }

    else{
    JS Code
    }

else if Statement Example


<html>
 <head>
  <title> If Else statement </title>
 </head>
 <body>

   <script type="text/javascript">

    var a=5;
     
    if(a==10) {
      document.write("a is = 10");
    }
    else if(a==5) {
      document.write("a is = 5");
    }
    else{
      document.write("a is not = 10 and 5");
    }

   </script>
 </body>
</html>

Results

JavaScript Comparison Operators :

Comparison Operators are used in JavaScript and in other Programming Languages to get equality or difference between variables or values.

Operator Description Example
== equal to a==1
!= not Equal a!=2
> greater then a>b
< less then b<a
>= greater then or equal a>=1
<= less then or equal a<=10

Equal to Operator ( == ):

Equal to Operator ( == )compares two values and return True and False values, If given value is equal to compared value then operator will return True or if given value is not equal to compared value then Operator will return False.

Equal to Operator


    var a=6;

    if(a>5){
      document.write("a is greater then 5");
    }
     else{
      document.write("a is not greater then 5");
    }

Results :

Not equal to Operator ( != ) :

Not equal to div compares two value and return True and False value. If value is not equal to another value then the Operator will returns True and if given value is equal to another value then Operator will return False.

Not equal to Operator


    var a=6;

    if(a!=5){
      document.write("a is not = 5");
    }
     else{
      document.write("a is = 5");
    }

Results


Greater Then Operator (>) :

Greater the Operator compares two value and return that variable or value who is greater then other variable or value.

Greater Then Operator:


    if(5>2){
      document.write("5 is greater then 2");
    }
      else{
      document.write("2 is greater the 5");
    }

Results :


Less Then Operator (>) :

Greater the Operator compares two value and return that variable or value who is Less then other variable or value.

Less Then Operator:


    if(2<5){
      document.write("2 is less then 5");
    }
      else{
      document.write("5 is less then 2");
    }

Results :


greater then or equal to ( >= ):

This operator compares two value for 2 things, 1st value is greater or not and 2nd value is equal or not and if value is greater then operator will return true and if value is not greater then but equal to another variable/value then also operator will returns True but if variable/value is not greater and also not equal to another value/variable then operator will return False

greater or equal operator :


    if(7>=7){
      document.write("7 is greater or equal to 7");
    }
      else{
      document.write("7 is not greater and equal to 7");
    }

Results :

Here 7 is not greater then 7 but 7 is equal to 7. Program will reacts the same for less or equal to Operator, if value/variable is less or equal to another value/variable then operator will returns TRUE.


Logical Operators :

Logical operators determines logical deference between two variables/values

Operator Description Example
&& And (7>5 || 7<10)
|| or (2>5 || 2<3)

And Operator ( a<5 && a>10 )

And Operator compares two value for two different given conditions at the same time and if both of the given Condition are true then Operator will returns True and if one of the 2 condition or both given conditions are false the Operator will return False.

And Operator :


    var a=7,b=5;
    if(a>b && a!=b){
      document.write("Variable a is greater and not equal to Variable B");
    }
      else{
      document.write("a is not greater or equal to B");
    }	

Results :

Or Operator (a>b || a<b):

Or Operator compares two value for 2 Conditions, if one of the two condition or both conditions return true then Or Operator Return True but Or operator will only returns False if both Conditions return false.

Or Operator :


    var a=7,b=5;
    if(a>b && a==b){
      document.write("Variable a is greater or equal to Variable B");
    }
      else{
      document.write("a is not greater and equal to B");
    }	

Results :

If Else Statement Example


<html>
 <head>
  <title> JavaScript If Else Statements </title>
 </head>
 <body>

   <script type="text/javascript">

    var a=7,b=5,c=0;

    if(a<b && a>c){
       document.write("a is less then b and greater then c");
    }
    else if(a<b || a>c){
       document.write("a is less then b or greater then c");     
    }
    else{
       document.write("a is not less then b and not greater then c");
    }

   </script>
 </body>
</html>	

Results

Comments

minecraft download pc

http://www.jamesdoub.com/comment/html/?107988.html
07:20 pm

Hi there, just became aware of your blog through Google, and found that it's truly informative. I'm going to watch out for brussels. I will be grateful if you continue this in future. A lot of people will be benefited from your writing. Cheers!

download minecraft free

https://www.yuxianfa.com/space-uid-331363.html
03:52 am

Thanks , I've just been searching for information about this topic for ages and yours is the greatest I have came upon till now. However, what in regards to the conclusion? Are you positive about the source?

download minecraft free

http://tinyurl.com/y4ommufk
05:14 am

I've been surfing on-line more than three hours these days, yet I by no means discovered any attention-grabbing article like yours. It's beautiful value sufficient for me. Personally, if all website owners and bloggers made just right content material as you did, the internet will probably be a lot more helpful than ever before.

minecraft pc download

http://tinyurl.com/y5u9c2mp
07:41 am

Pretty nice post. I just stumbled upon your blog and wanted to say that I've really enjoyed surfing around your blog posts. After all I'll be subscribing to your feed and I hope you write again soon!

minecraft download for free

http://tinyurl.com/y4anjgna
07:11 am

These are really enormous ideas in concerning blogging. You have touched some good points here. Any way keep up wrinting.

download minecraft for free

http://tinyurl.com/y5ja9kes
09:18 am

I don't even understand how I stopped up right here, however I believed this post used to be great. I don't know who you're but certainly you're going to a well-known blogger when you aren't already. Cheers!

g

http://tinyurl.com/y6aro6x8
01:18 pm

I'm really enjoying the design and layout of your website. It's a very easy on the eyes which makes it much more pleasant for me to come here and visit more often. Did you hire out a designer to create your theme? Fantastic work!

g

http://t.co/fdcdhCkdIu
08:39 pm

Hello there! I know this is kinda off topic but I was wondering if you knew where I could locate a captcha plugin for my comment form? I'm using the same blog platform as yours and I'm having problems finding one? Thanks a lot!

minecraft download for free

http://tinyurl.com/y5v5m7gr
03:08 am

Thanks for ones marvelous posting! I actually enjoyed reading it, you happen to be a great author.I will be sure to bookmark your blog and may come back at some point. I want to encourage you to definitely continue your great writing, have a nice day!

free minecraft

http://tinyurl.com/y5beu3yh
06:21 pm

Good day very nice website!! Man .. Excellent .. Amazing .. I will bookmark your website and take the feeds additionally? I am happy to search out so many helpful info here within the post, we'd like work out extra strategies in this regard, thanks for sharing. . . . . .

mojang minecraft download

http://t.co/jnBGbHSIRT
11:07 pm

Wow, that's what I was searching for, what a information! existing here at this web site, thanks admin of this web site.

minecraft free download

http://t.co/mNpGUufEwk
01:32 am

What's up mates, good paragraph and pleasant arguments commented at this place, I am in fact enjoying by these.

gamefly free trial

http://tinyurl.com/y3nljof8
10:46 am

Appreciating the dedication you put into your site and detailed information you provide. It's awesome to come across a blog every once in a while that isn't the same old rehashed information. Excellent read! I've saved your site and I'm including your RSS feeds to my Google account.

gamefly

http://t.co/5Rc47F3eqS
06:27 pm

Today, while I was at work, my cousin stole my iphone and tested to see if it can survive a thirty foot drop, just so she can be a youtube sensation. My iPad is now destroyed and she has 83 views. I know this is entirely off topic but I had to share it with someone!

gamefly

http://t.co/KdjHA1ipGy
12:19 am

Aw, this was an exceptionally good post. Taking the time and actual effort to make a good article_ but what can I say_ I procrastinate a lot and don't manage to get nearly anything done.

Write to Us