5.0 Chap 5 - Basic Client-Side Scripting
5.0 Chap 5 - Basic Client-Side Scripting
Basic Client-Side
Scripting ( JavaScript )
Prepared for:
CSC264 – Introduction to Web and Mobile Application
OVERVIEW OF THIS CHAPTER
Binary
Operator
Unary
Operator
JAVASCRIPT: OPERATORS
Operators:
➢ There are many types of variables, which include:
2. Arithmetic operators:
▪ Increment and Decrement operators can be evaluated in 2 forms:
i. Pre-fix Form (Before):
The value in the variable will be increased/decreased before the
value in the variable is used for calculation/evaluation
Operator Remark
Highest level - Negative
() Parenthesis
*, /, Multiply, Divide,Modulus
Lowest level +, - Add, Subtract
Same level
JAVASCRIPT: OPERATORS
Operators:
➢ There are many types of operators, which include:
3. Comparison operators:
▪ Are used to compare between 2 values or variables
▪ The result of the comparison will either be:
• True
• False
variable.toFixed(x)
➢ The number will be rounded up, and nulls are added after the decimal
point (if needed) to create the desired decimal length
EXAMPLE
JAVASCRIPT: SPECIAL NUMBERS
Numbers:
➢ Below is a list of special numbers supported by JavaScript:
EXAMPLE
JAVASCRIPT: MATH OBJECT
Math Object:
➢ To perform mathematical tasks on numbers, Math object can be used:
Math.method()
EXAMPLE
JAVASCRIPT: CONSTANT
Math Object:
➢ With the Math object, you can also use the constant that it provides:
Math.constant()
EXAMPLE
JAVASCRIPT: DATE AND TIME
Date and Time:
➢ To work with date and time, a Date object needs to be created
➢ A date consists of:
A year,a month, a day,an hour, a minute, a second, and milliseconds
➢ The syntax used is:
variable = new Date();
JAVASCRIPT: DATE AND TIME
Date and Time:
➢ The following method can be used on a Date object: Date.method()
EXAMPLE
switch(expression)
{
case 1:
execute this code block
break;
case 2:
execute this code block
break;
default:
otherwise execute this code block
}
EXAMPLE
EXAMPLE
2. while Loop:
Will repeat a block of code while a specified condition is true
initial_statement;
while(condition)
{
//code to be executed
update_statement;
}
JAVASCRIPT: REPETITION
Repetition Structure (Loop):
1. for Loop:
for(var i=1; i<=5; i++)
{
document.write(“<p>The number is “ + i + “</p>”);
}
2. while Loop:
var i=1;
while(i<=5)
{
document.write(“<p>The number is “ + i + “</p>”);
i++;
}
JAVASCRIPT: REPETITION
initial_statement;
do
{
//code to be executed
update_statement;
}
while(condition);
JAVASCRIPT: REPETITION
var i = 6;
do
{
document.write(“<p>The number is “ + i + “</p>”);
i++;
}
while(i <=5);
JAVASCRIPT: ARRAY
Array:
➢ Is a special variable, which can hold more than one value at a time
➢ The syntax to declare an array without a fixed size:
Array:
➢ To assign initial values to an array, you can use any of the following ways:
This is better
JAVASCRIPT: ARRAY
Array:
➢ To assign values to an array,you need to use the“index number”of the array
Method Description
concat( ) Combines the elements of two or more arrays into one new array
join( ) Combines the elements of an array into a single string with a separator character
pop( ) Removes the last element
push( ) Adds elements to the end of an array
reverse( ) Reverse the direction of the elements
shift( ) Remove the first element of an array
unshift( ) Adds elements to the beginning
slice( ) Pulls out the specified section of an array
splice( ) Removes elements from an array or replace elements in an array
sort( ) Sorts elements of an array into alphabetical order based on the string values of the
elements
Str
JAVASCRIPT: FUNCTIONS
Function:
➢ QUICK QUESTION:
Is a group of instructions intended to perform a particular task
What is the
➢ To use a function, you need to use the function keyword advantage of using
function?
➢ The syntax for function definition is:
➢ The statement inside a function will only be executed when a function call is
performed
➢ The syntax for function call is:
functionName (parameters);
EXAMPLE
Function
Definition
Function Call
JAVASCRIPT: FUNCTIONS
Function as Data:
➢ Function can also be assigned to variables