






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







Image you need to keep track of the scores of students in a classand you have 50 students
Declaring and handling 50 variables is not an easy task
Arrays come to the rescue
Array – Collection of values that can be treated as a unit andwhere you can handle each individual entry (similar to an eggcarton)
You can visualize an array as a set of variables one after another
There are several ways to define arrays. var scienceScores = new Array();
// Creates an empty array
var mathScores = new Array(3);
// Creates an array with 3 entries
var englishScores = [77, 88, 65];
// Creates an array with 3 entries// having the specified values
To access elements of an array
Use the [ ] operator
Integer Index values
starting at zero
will represent
each element
Accessing array elements mathScores[0] = 70;
// Assigning 70 to the first array element
mathScores[1] = 80;
// Assigning 80 to the second array element
var total = mathScores[0] + mathScores[1];
// reading the first
// and second elements
Property – Entity with a name and a value
In JavaScript an object is a composite type whichaggregates several properties
Object properties work like variables and fundamentallyare variables. You can assign values to them and readvalues from them
A property value can be any data type we have seen,including objects
An example of an object is the
navigator
object which provides information
about the web browser
Among the
navigator
object properties you can find
appName – web browser name
appCodeName code name for the browser
You can access properties by specifying the object name, a period, and theproperty name or by specifying the property name using array-like syntax
navigator.appNamenavigator[“appName”]
Notice that you can read of modify properties using the above notation
Example 3
(See Example3.html)
By making use of functions, object properties and formswe can now define an html document with JavaScriptthat generates customized multiplication tables
Example 5
(See Example5.html)
Notice how we can pass properties of elements of theform to the multiplicationTable function
method is a function that is invoked through an object.
The window object has several interesting methods
window.open() – creates and opens a window
window.print() – equivalent to clicking on thebrowser’s Print button
window.back() – equivalent to clicking on the browser’s Back button
window.forward() – equivalent to clicking on thebrowser’s Forward button
JavaScript (Examples on the Web
Let’s take a look at some JavaScript availableon the web
Set 1 http://www.bodo.com/javacool.htm
Set 2 http://www.crowes.f9.co.uk/Javascript/index.htm