Bem-vindo ao JavaScript Tutorial
Previous topic/Tópico anterior
Next topic/Próximo tópico
04 - if conditional expressions

Implement the use of conditional expressions.


<html>
<body>

<script>

bananas = 6

if (bananas == 6)
{
	alert("It's true. We have a half dozen of bananas.")
}

</script>

</body>
</html>
The if statement is the most basic conditional expressions in JavaScript, and yet the most used. With it you can decide to execute or not an action.

Understanding the code:

bananas = 6
Makes the "bananas" variable receive number 6.

if (bananas == 6)
{
	alert("It's true. We have a half dozen of bananas.")
}
The code above is a if conditional expression.

Let's analyze it:

if (bananas == 6)
{
	alert("It's true. We have a half dozen of bananas.")
}
This is the conditional expression. If it evaluates to be true (in the case above, if bananas is equal to six), we will be entering and executing the code right below it. Next, we will see the structure of a code block.

if (bananas == 6)
{
	alert("It's true. We have a half dozen of bananas.")
}
Opens the code block. All the code inside it will be executed. Remember, it will be executed only if the conditional statement evaluates to be true.

if (bananas == 6)
{
	alert("It's true. We have a half dozen of bananas.")
}
Give us a small alert window with the following text: It's true. We have a half dozen of bananas.

if (bananas == 6)
{
	alert("It's true. We have a half dozen of bananas.")
}
Closes the code block.

You can make tests yourself, changing the "bananas = 6" to "bananas = 10" or any number that isn't 6.
If you do it, the conditional statement will not evaluate to true, and it will simply not enter inside the code block that does the alert("It's true. We have a half dozen of bananas.").

Previous topic/Tópico anterior
Next topic/Próximo tópico
[ Voltar ]


© 2005 JavaScript Tutorial Brasil
Proibida a cópia ou distribuição integral ou parcial não-autorizada
Unauthorized integral or partial distribution or reproduction is forbidden
Patrocínio - Assistência Técnica de Calculadoras HP CSCenter