JavaScript-Part Two
Content
JavaScript Variables
JavaScript Operators
o JavaScript Arithmetic
Example
o JavaScript Assignment Operators
o The + Operator Used on Strings
o Adding Strings and Numbers
Comparison Operators
Logical Operators
JavaScript If...Else Statements
JavaScript Switch Statement
JavaScript For Loop
JavaScript While Loop
The do...while Loop
1 web programming
JavaScript Variables
JavaScript variables are used to hold values or expressions. A variable can have a short name, like x,
or a more descriptive name, like carname.
Rules for JavaScript variable names:
Variable names are case sensitive (y and Y are two different variables)
Variable names must begin with a letter or the underscore character
Note: Because JavaScript is case-sensitive, variable names are case-sensitive.
Declaring (Creating) JavaScript Variables
Creating variables in JavaScript is most often referred to as "declaring" variables.
You can declare JavaScript variables with the var statement:
After the declaration shown above, the variables are empty (they have no values yet). However, you
can also assign values to the variables when you declare them.
Note: When you assign a text value to a variable, use quotes around the value.
2 web programming
Assigning Values to Undeclared JavaScript Variables
If you assign values to variables that have not yet been declared, the variables will automatically be
declared.
These Statements:
has the same effect as:
JavaScript Arithmetic
As with algebra, you can do arithmetic operations with JavaScript variables:
JavaScript Arithmetic Operators
Arithmetic operators are used to perform arithmetic between variables and/or values. the table below
explains the arithmetic operators.
Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus ( finding the remainder )
++ Increment
-- Decrement
3 web programming
Example:
Given that x=10 and y=5, the table below explains the assignment operators:
HTML & JavaScript
<html>
<head>
<script type="text/JavaScript">
var two = 2
var ten = 10
var linebreak = "<br />"
document.write("two plus ten = ")
var result = two + ten
document.write(result)
document.write(linebreak)
document.write("ten * ten = ")
result = ten * ten
document.write(result)
document.write(linebreak)
document.write("ten / two = ")
result = ten / two
document.write(result)
</script>
</head>
<body>
</body>
</html>
4 web programming
Output
JavaScript Assignment Operators
Assignment operators are used to assign values to JavaScript variables.
Example:
Given that x=10 and y=5, the table below explains the assignment operators:
5 web programming
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. For example
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:
After the execution of the statements above, the variable txt3 contains:
"What a very nice day" (Note the space is available)
Adding Strings and Numbers
Note: The rule is: If you add a number and a string, the result will be a string!
<html>
<head>
<script type="text/JavaScript">
x=5+5;
document.write(x +"<br>");
x="5"+"5";
document.write(x+"<br>");
x=5+"5";
document.write(x+"<br>");
x="5"+5;
document.write(x+"<br>");
</script>
</head>
<body>
</body>
</html>
6 web programming
Comparison Operators
Comparison and Logical operators are used to test for true or false.
Comparison operators are used inside conditional statements to determine equality or difference
between variables or values.
Operator Description
== Equal To
=== Exactly equal to (value and type)
!= Not Equal To
> Less than
< Greater than
<= Less than or equal
>= Greater than or equal
Note : A single equal (=) sign sets a value which means assignment operator.
Given that x=5, the table below explains the comparison operators:
Operator Example
== X= =8 is false
=== X= = =5 is true
X= = = “5” is false
!= X != 8 is true
> x>8 is false
< X< 8 is true
<= X<= 8 is true
>= X>= 8 is false
How Can it be Used
Comparison operators can be used in conditional statements to compare values and take action
depending on the result:
7 web programming
Logical Operators
Logical operators are used to determine the logic between variables or values.
Given that x=6 and y=3, the table below explains the logical operators:
JavaScript If...Else Statements
Conditional statements are used to perform different actions based on different conditions.
Conditional Statements
In JavaScript we have the following conditional statements:
if statement - use this statement to execute some code only if a specified condition is true.
Syntax
if (condition)
{
//code to be executed it the condition is true
}
Note: if is written in lowercase letters. Using uppercase letters (IF) will generate a JavaScript error!
Example:
8 web programming
if...else statement - use this statement to execute some code if the condition is true and
another code if the condition is false.
Syntax:
Example:
9 web programming
if...else if....else statement - use this statement to select one of many blocks of code to be
executed.
Syntax
Example
10 web programming
switch statement - use this statement to select one of many blocks of code to be executed.
Syntax
This is how it works: First we have a single expression n (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:
11 web programming
JavaScript For Loop
Loops execute a block of code a specified number of times, or while a specified condition is true.
JavaScript Loops
Often when you write code, you want the same block of code to run over and over again in a row.
Instead of adding several almost equal lines in a script we can use loops to perform a task like this.
In JavaScript, there are different kind of loops:
for - loops through a block of code a specified number of times
while - loops through a block of code while a specified condition is true
Do..while - loops through a block of code ONCE, and then it will repeat the loop as long as
the condition is true
for Loop
The for loop is used when you know in advance how many times the script should run.
Syntax
Example
The example below defines a loop that starts with i=0. The loop will continue to run as long as i is
less than, or equal to 5. i will increase by 1 each time the loop runs.
Note: The increment parameter could also be negative, and the <= could be any comparing
statement.
12 web programming
While Loop
Loops execute a block of code a specified number of times, or while a specified condition is true.
The while Loop
The while loop loops through a block of code while a specified condition is true
Syntax
Example
The example below defines a loop that starts with i=0. The loop will continue to run as long as i is
less than, or equal to 5. i will increase by 1 each time the loop runs:
The do...while Loop
The do...while loop is a variant of the while loop. This loop will execute the block of code ONCE,
and then it will repeat the loop as long as the specified condition is true.
Syntax
13 web programming
Example
Note: The do...while loop will always be executed at least once, even if the condition is false,
because the statements are executed before the condition is tested:
14 web programming