Arrays and Functions

Call-by-value

In JavaScript when an individual
argument is passed to a function it is passed using "Call-by-value"

In call-by-value, the called function is passed a variable's value and copies it
to the automatic variables in its
argument list.

Advantage: The called function cannot change the values
of the corresponding variables in the calling function.

Disadvantage: If a large object is passed, copying it can take extra memory
and execution time.

Call-by-reference

Arrays (and other Objects) in JavaScript use "Call-by-reference" when passed to a function.

Call-by-Reference does not make copies of original data. This allows functions to access and manipulate common data resources.

Call-by-value: transmits a copy of a value

Call-by-reference: transmits an address

Passing
Arrays to Functions

To pass an array element as an argument to a function,
specify the individual element (passed call-by-value). someFunction(response[4]);

To pass an entire array as an argument to a function,
specify the name of the array without any brackets (passed call-by-reference) someFunction(response);

To receive an array, the function parameter list must
specify that a argument will be received and then that argument must be
used as an array within the function function someFunction(r) { document.write(r.length);}

Here we use methods like getHours() in order
to display the time and date specified in out Date-object now. You can see
that we are adding 1 to the month. The method getMonth()
returns the number 0 for January so we need to add 1 to get the correct
month.

This script checks whether the number of minutes is less than 10.
This is so you don't get a time which looks like this: 14:3 when you want 14:03.

To make this into a working clock we would add a timer to the function: Timer= setTimeout("myclock()",1000); the setTimeout() method to call the myclock()
function every 1000 milliseconds.

A String may be assigned to a variable in a declaration.
The declaration var color = "blue";
color = String("blue");
Either statement initializes variable color as a String object containing
the string "blue".

Strings can be compared with the relational operators (<, <=,
> & >=) and the equality operators (==
& !=).

String-object Methods

Some String-object methods are:

indexOf(substring, index) Searches for the first
occurrence of substring starting from position index.

Returns starting index of substring (-1 if not
found).

split(delimeter) Splits the string
into an array of strings splitting at every delimiter:
(e.g. sArray = string1.split(" ");)