Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Javascript IF: Conditional Structures in Javascript

In this article we will see how to work with the IF structure in Javascript also see how else to use real-world examples.

Hello guys, in this article we will see how to work with some conditional structures in javascript. The IF and ELSE.

JavaScript (sometimes abbreviated JS) is a scripting language commonly implemented as part of a web browser in order to create enhanced user interfaces and dynamic websites.

Javascript

Figure 1: Javascript

If in Javascript

IF is a control structure used to make decisions. Performs one or more operations on the basis of an expression in the form of condition. Basically first is given a condition and if this condition is positive, it performs the action.

The basic structure is the if in javascript: if (condition) {code to be executed}.

This condition is made by logical operators such as larger, smaller, less equal, equal, greater equal, different. Can also be used in conjunction with or not: and, or, etc..

Let's see how this works in practice? In the example below we make a condition checking whether a number is equal to another if the condition is true we will use document.write to print a message.

Listing 1: Example of If in Javascript

<script language="JavaScript" type="text/javascript">
 
  if (25 == 25) {
    document.write ("The numbers are the same");
  }
 
</script>
 

Using this way, we assume that there is only one chance and that the condition will always be true, because if not, nothing will happen. To define a command to run the program if the condition is negative else's need, the next example we see how else does.

Else in Javascript

What Else basically serves to supply the negative part of the IF condition, ie, when the IF condition is false, the ELSE assumes and performs the action.

Its syntax is equal to the IF, but with a slight difference: if (condition) {code executed if true} else {code executed if false}.

Let's take the previous example of the IF and ELSE add it, seeing how it works:

Listing 2: Example of usage in javascript Else

<script language="JavaScript" type="text/javascript">
if (25 == 35) {
    document.write ("The numbers are the same");
        } else {
            document.write ("The numbers aren’t the same ");
        }
</script>

As we can see in the above code, the numbers are not equal, then the condition will be executed else, this example is somewhat generic because from the beginning we know that the result will be negative, because they are values ​​set by us. But this is very useful when used values ​​coming from variables, which are totally dependent on user interaction.

Let's see another example where it becomes more evident the importance of if and else.

Listing 3: Example of real world if and else

<script language="JavaScript" type="text/javascript">
var idade;
if (idade >= 18) {
    document.write ("You have more than 18 years old");
        } else {
            document.write ("You have less than 18 years old ");
        }
</script>

In this example, we will depend on what the value of age to enter the user know if the condition to be executed will be positive or negative.

You can use the logic of you and test several different ways to do this kind of condition, feel free to send your questions in the comments I'll be happy to help them.

I hope you enjoyed and until the next tutorial.



Front-end developer, WebEditor of MrBool.com and SEO Analyst. Work in the development area for over 4 years. For freelance work visit my portfolio: www.ricardoarrigoni.com.br

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login