Sei sulla pagina 1di 17

JAVASCRIPT

ARRAY–PART 5
DFT 3013 : WEB DESIGN TECH
LIYANA MAT RANI, PBU
2
POP-PUP BOX

▸Define an array
▸Explain types of array
▸Declare an array
▸Create program using an array structure
3

ARRAY
4
JAVASCRIPT ARRAY

▸JavaScript arrays are used to store multiple values


in a single variable.
▸Javascript only support numeric/index array.
5
ARRAY

ARRAY DECLARATION

With JavaScript, the


full array can be
accessed by referring
to the array name- no
looping need if you use
this way
6
USING THE KEYWORD NEW TO CREATE ARRAY
▸The following example also creates an Array, and assigns
values to it:

Here and previous


example do exactly the
same. There is no need
to use new Array().
For simplicity, readability
and execution speed, use
the first one (the array
literal method).
7
Access the Elements of an Array

Same like other


programming language, to
access an array we use
index value.

What is the output here ?


8
ARRAYS ARE OBJECT

▸Arrays are a special type of objects.


▸The typeof operator in JavaScript returns "object" for arrays.
▸But, JavaScript arrays are best described as arrays.
▸So since array is an object,unlike array other programming language,
JavaScript can store different data types in single declaration.
▸Arrays use numbers to access its "elements". In this
example, person[0] returns John:
9
ARRAYS ARE OBJECT
10
ARRAYS ARE OBJECT

Objects use names to access its "members".

Array declared with name

Access the array using name


11
ARRAY PROPERTIES & METHODS

JavaScript arrays also have own properties and Methods.


Example of methods in Array:

.length; // The length property returns the number of elements


.sort(); // The sort() method sorts arrays
.toString(); // Converts an array to a string of array values.
.push() // Method adds a new element to an array (at the end).
.pop() // Method removes the last element from an array).
12
ARRAY PROPERTIES & METHODS

What is the output here ?


13
Looping Array Elements

Combine with list


14
ADDING / CHANGE ELEMENT IN THE ARRAY
15
ADDING ELEMENT IN THE ARRAY

What is the output here ?


16
ADDING ELEMENT IN THE ARRAY

What is the output here ?


17
EXERCISE

▸EXERCISE 1
▸EXERCISE 2
▸EXERCISE 3
▸EXERCISE 4
▸EXERCISE 5

Potrebbero piacerti anche