Monday, 1 August 2011

Part 3 - Operators & IF Statements | JavaScript

JavaScript Operators

= is used to assign values.
+ is used to add values.

The assignment operator = is used to assign values to JavaScript variables.
The arithmetic operator + is used to add values together.
y=5;
z=2;
x=y+z;
The value of x, after the execution of the statements above is 7.

JavaScript Arithmetic Operators

Arithmetic operators are used to perform arithmetic between variables and/or values.
Given that y=5, the table below explains the arithmetic operators:
Operator Description Example Result
+ Addition x=y+2 x=7
- Subtraction x=y-2 x=3
* Multiplication x=y*2 x=10
/ Division x=y/2 x=2.5
% Modulus (division remainder) x=y%2 x=1
++ Increment x=++y x=6
-- Decrement x=--y x=4


JavaScript Assignment Operators

Assignment operators are used to assign values to JavaScript variables.
Given that x=10 and y=5, the table below explains the assignment operators:
Operator Example Same As Result
= x=y   x=5
+= x+=y x=x+y x=15
-= x-=y x=x-y x=5
*= x*=y x=x*y x=50
/= x/=y x=x/y x=2
%= x%=y x=x%y x=0


The + Operator Used on Strings

The + operator can also be used to add string variables or text values together.
To add two or more string variables together, use the + operator.
txt1="What a very";
txt2="nice day";
txt3=txt1+txt2;
After the execution of the statements above, the variable txt3 contains "What a verynice day".
To add a space between the two strings, insert a space into one of the strings:
txt1="What a very ";
txt2="nice day";
txt3=txt1+txt2;
or insert a space into the expression:
txt1="What a very";
txt2="nice day";
txt3=txt1+" "+txt2;
After the execution of the statements above, the variable txt3 contains:
"What a very nice day"

Adding Strings and Numbers

The rule is: If you add a number and a string, the result will be a string!

Example

x=5+5;
 document.write(x);
 x="5"+"5";
 document.write(x);
 x=5+"5";
 document.write(x);
 x="5"+5;
 document.write(x); 
 

If Statement

If


The if function allows you to check to see if something is true or false. For example you could check to see if text entered by a user matches a piece of text you already have (like a password). To show if in action click here.

As you can see this could be very useful for many sites. The code is as follows:

 var guess = prompt("Please guess a number between 1 and 10","");
 if(guess == 5)
 {
 alert('Correct! I was thinking of 5');
 }
 else
 {
 alert('Wrong! I was thinking of 5');
 }

This code is made up of three main parts. The first part which gets the guess from the user, you have used before. This is followed by:

 if(guess ==5)

Which is quite self explanitary. It checks to see if the variable guess is equal to 5. The if statement is followed by:

 {
 alert('Correct! I was thinking of 5');
 }

The important part of this is the curly brackets round the alert command. These contain the code which should be executed if the if statement returns 'true' (in this example if guess equals 5). The final part is the:

 else
 {
 alert('Wrong! I was thinking of 5');
 }

This tells the browser that if the if statement does not return 'true' (in this example if guess does not equal 5) to execute the code between the curly brackets.

Together this code makes up the if statement.

More If


There are other conditions you can test with the if statement. Firstly, as well as using numbers you can compare variables or text:

 if(variable == variable)
 
 if(variable == "some text")

As well as doing this you can check to see if one variable is greater than another, less than another or not the same as another:

if(variable < othervariable)
If variable is less than othervariable

 if(variable > othervariable)
If variable is greater than othervariable

 if(variable <= othervariable)
If variable is less than or equal to othervariable

 if(variable >= othervariable)
If variable is greater than or equal to othervariable

 if(variable != othervariable)
If variable is not equal to other variable

These can be very useful in your web pages. You can also check to see if two conditions are true before executing code using &&:

 if(variable1 == variable2) && (variable1 < variable3)

This will only execute its code if variable1 is equal to variable2 and is less than variable3. You can also run the code if one of the conditions is true:

 if(variable1 == variable2) || (variable1 < variable3)

This will only execute its code if variable 1 is equal to variable to or is less than variable3.

Else If Statement

In the previous lesson, you learned how to create a basic If Statement in JavaScript, which is good enough for most programming situations. However, sometimes it is helpful to have the ability to check for more than one condition in a single If Statement block.
The Else If statement is an extension to the If Statement that allows you to create as many checks (conditional statements) as you want in one big block of If Statement code.

JavaScript Else If Example

Imagine that you want to have a small "student" script that will print out a customized message depending who is accessing the webpage. If you have more than two custom messages, you could use the Else If extension to solve this programming problem.

JavaScript Code:

<script type="text/javascript">
<!--
var visitor = "principal";
if(visitor == "teacher"){
 document.write("My dog ate my homework...");
}else if(visitor == "principal"){
 document.write("What stink bombs?");
} else {
 document.write("How do you do?");
}
//-->
</script>

Display:

What stink bombs?
There are two important things to note about the Else If extension:
  1. You must have a normal If Statement before you can use the Else If statement. This is because the Else If statement is an addon to the If Statement.
  2. You can have multiple Else If add-ons. In our example, we only used one Else If extension, but you can add as many as you require.

GA_googleFillSlot('Tizag_Javascript_Content_468x60');

0 comments em “Part 3 - Operators & IF Statements | JavaScript”

Post a Comment

 

E-Learning Copyright © 2011 Powered by Blogger