|
JavaScript
Conditional Statements
Conditional
statements in JavaScript are used to perform
different actions based on different
conditions.
Conditional
Statements
Very often
when you write code, you want to perform
different actions for different decisions. You
can use conditional statements in your code to
do this.
In JavaScript
we have three conditional statements:
- if
statement - use
this statement if you want to execute a
set of code when a condition is true
- if...else
statement - use
this statement if you want to select one
of two sets of lines to execute
- switch
statement - use
this statement if you want to select one
of many sets of lines to execute
If
and If...else Statement
You should
use the if statement if you want to execute
some code if a condition is true.
Syntax
if (condition)
{
code to be executed if condition is true
}
|
Example
//If the time on your browser is less than 10,
//you will get a "Good morning" greeting.
<script type="text/javascript">
var d=new Date()
var time=d.getHours()
if (time<10)
{
document.write("<b>Good morning</b>")
}
</script>
|
Notice that
there is no ..else.. in this syntax. You just
tell the code to execute some code if
the condition is true.
If you want
to execute some code if a condition is true
and another code if a condition is false, use
the if....else statement.
Syntax
if (condition)
{
code to be executed if condition is true
}
else
{
code to be executed if condition is false
}
|
Example
//If the time on your browser is less than 10,
//you will get a "Good morning" greeting.
//Otherwise you will get a "Good day" greeting.
<script type="text/javascript">
var d = new Date()
var time = d.getHours()
if (time < 10)
{
document.write("Good morning!")
}
else
{
document.write("Good day!")
}
</script>
|
Switch
Statement
You should
use the Switch statement if you want to select
one of many blocks of code to be executed.
Syntax
switch (expression)
{
case label1:
code to be executed if expression = label1
break
case label2:
code to be executed if expression = label2
break
default:
code to be executed
if expression is different
from both label1 and label2
}
|
This is how
it works: First we have a single expression
(most often a variable), that is evaluated
once. The value of the expression is then
compared with the values for each case in the
structure. If there is a match, the block of
code associated with that case is executed.
Use break to prevent the code from
running into the next case automatically.
Example
//You will receive a different greeting based
//on what day it is. Note that Sunday=0,
//Monday=1, Tuesday=2, etc.
<script type="text/javascript">
var d=new Date()
theDay=d.getDay()
switch (theDay)
{
case 5:
document.write("Finally Friday")
break
case 6:
document.write("Super Saturday")
break
case 0:
document.write("Sleepy Sunday")
break
default:
document.write("I'm looking forward to this weekend!")
}
</script>
|
Conditional
Operator
JavaScript
also contains a conditional operator that
assigns a value to a variable based on some
condition.
Syntax
variablename=(condition)?value1:value2
|
Example
greeting=(visitor=="PRES")?"Dear President ":"Dear "
|
If the
variable visitor is equal to PRES, then put
the string "Dear President " in the
variable named greeting. If the variable
visitor is not equal to PRES, then put the
string "Dear " into the variable
named greeting.
JavaScript
Looping
Looping
statements in JavaScript are used to execute
the same block of code a specified number of
times.
Looping
Very often
when you write code, you want the same block
of code to run a number of times. You can use
looping statements in your code to do this.
In JavaScript
we have the following looping statements:
- while
- loops through
a block of code while a condition is true
- do...while
- loops through a block of code once, and
then repeats the loop while a condition is
true
- for
- run statements
a specified number of times
while
The while
statement will execute a block of code while a
condition is true..
while (condition)
{
code to be executed
}
|
do...while
The
do...while statement will execute a block of
code once, and then it will repeat the loop
while a condition is true
do
{
code to be executed
}
while (condition)
|
for
The for
statement will execute a block of code a
specified number of times
for (initialization; condition; increment)
{
code to be executed
}
|
JavaScript
String Object
The
String object is used to work with text.
The
Most Common Methods
Methods
|
Explanation
|
NN
|
IE
|
| length |
Returns
the number of characters in a string |
2.0 |
3.0 |
| index Of() |
Returns
the index of the first time the
specified character occurs, or -1 if
it never occurs, so with that index
you can determine if the string
contains the specified character. |
2.0 |
3.0 |
| lastIndexOf() |
Same
as index Of, only it starts from the
right and moves left. |
2.0 |
4.0 |
| match() |
Behaves
similar to indexOf and lastIndexOf,
but the match method returns the
specified characters, or
"null", instead of a numeric
value. |
4.0 |
4.0 |
| substr() |
Returns
the characters you specified: (14,7)
returns 7 characters, from the 14th
character. |
4.0 |
4.0 |
| substring() |
Returns
the characters you specified: (7,14)
returns all characters between the 7th
and the 14th. |
2.0 |
3.0 |
| toLowerCase() |
Converts
a string to lower case |
2.0 |
3.0 |
| toUpperCase() |
Converts
a string to upper case |
2.0 |
3.0 |
An
Array object is used to store a set of values
in a single variable name.
The
Array object
An Array
object is used to store a set of values in a
single variable name. Each value is an element
of the array and has an associated index
number.
You can refer
to a particular element in the array by using
the name of the array and the index number.
The index number starts at zero.
You create an
instance of the Array object with the
"new" keyword.
var family_names=new Array(5)
|
The expected
number of elements goes inside the
parentheses, in this case 5.
You assign
data to each of the elements in the array like
this:
family_names[0]="Tove"
family_names[1]="Jani"
family_names[2]="Ståle"
family_names[3]="Hege"
family_names[4]="Kai"
|
And the data
can be retrieved from any element by using the
index of the particular array element you
want. Like this:
mother=family_names[0]
father=family_names[1]
|
The
Most Common Methods
Methods
|
Explanation
|
NN
|
IE
|
ECMA
|
| length |
Returns
the number of elements in an array.
This property is assigned a value when
an array is created
|
3.0 |
4.0 |
1.0 |
| reverse() |
Returns
the array reversed |
3.0 |
4.0 |
1.0 |
| slice() |
Returns
a specified part of the array |
4.0 |
4.0 |
|
| sort() |
Returns
a sorted array |
3.0 |
4.0 |
1.0 |
|