Chapter 15. JavaScript Functions, Objects, and Arrays
Just like PHP, JavaScript offers access to functions and objects. In JavaScript objects are the primary means for accessing the Document Object Model (DOM), because—as you’ve seen—every element of an HTML document is available to be manipulated as an object.
The usage and syntax are also quite similar to those of PHP, so you should feel right at home as I take you through using functions and objects in JavaScript, as well as through an in-depth exploration of array handling.
JavaScript Functions
In addition to having access to dozens of built-in functions (or methods), such as log, which you have already seen being used in console.log, you can easily create your own functions. A relatively complex piece of code that is likely to be reused is a candidate for a function.
Defining a Function
The general syntax for a function is:
functionfunction_name([parameter[,...]]){statements}
The first line of the syntax indicates:
-
A definition starts with the word
function. -
A name follows that must start with a letter or underscore, followed by any number of letters, digits, dollar signs, or underscores, the same rules as for any other identifier.
-
The parentheses are required.
-
One or more parameters, separated by commas, are optional (indicated by the square brackets, which are not part of the function syntax).
Like all identifiers in JavaScript, function names are case-sensitive, so all of the following strings refer ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access