Tuesday, December 17, 2013

JavaScript Tutorial 2 - If Else Statements

In JavaScript if else  statements play a huge part of the language the basic syntax for them looks like this..

if (Statement)

{
   Do something
}
else
{
   Do something else
}

Now to apply this in a normal situation you could have some user input and say if the user says "yes" then do a command and if they say "no" do something else. This application would look like this...

var q = prompt("Is your name Nick?");

if ( q == "yes")
{
alert("Hi Nick");
}
else
{
alert("oh ok"
}

The way the alert part works is similar to prompt except it doesn't have user input, it just displays a message. You must always use two "=" signs when comparing two different types of data (variable and string).

This is it for if else statements in JavaScript.

No comments:

Post a Comment