Presentation is loading. Please wait.

Presentation is loading. Please wait.

JavaScript Functions and Arrays

Similar presentations


Presentation on theme: "JavaScript Functions and Arrays"— Presentation transcript:

1 JavaScript Functions and Arrays
Chapter 20 JavaScript Functions and Arrays

2 JavaScript Functions Algorithm is a finite set of instructions that are general, consistent, finite, precise and take input and produce output Function is defined as a set of statements that takes input, use an algorithm, and produce output

3 JavaScript Functions function functionName ([param1, param2…]){
function statements goes here return output // optional alert(“hello”); // non-reached statement } // end of function // function call returnValue = functionName([param1, param2…]); * See example 20.1 ~ 20.4

4 JavaScript Array Array is an ordered set of values associated with a single variable name Array is a JavaScript predefined Object and “new” command to define it First element in array is 0 Defined array myArray = new Array(4); myArray = new Array (“red”,”green”,”blue”);

5 JavaScript Array Array object property – length Array object method
arrLength = myArray.length Array object method Reverse() Sort() Concat() Slice() Push() = add one element to the end of array Pop() = remove one element from the end of array See example 20.7

6 JavaScript Array Array Access – for … in statement var x
var mycars = new Array() mycars[0] = "Saab" mycars[1] = "Volvo" mycars[2] = "BMW" for (x in mycars){ document.write(mycars[x] + "<br />") } See Example 20.7

7 JavaScript Array Multi-dimensional Array
Two dimension array 2 rows 3 columns arr = new Array(new Array(2), new Array(3)); arr = new Array(new Array(3,9,2), new Array(5,1,7)); See example 20.6


Download ppt "JavaScript Functions and Arrays"

Similar presentations


Ads by Google