Defining Functions

  function add(a, b){     return a + b;   }   var result1 = add(5, 20);

You need to be familiar with creating and using functions in JavaScript. Functions are defined using the following syntax:

function function_name(arguments){function_block}

You can specify any number of arguments, and because JavaScript is loosely typed, you can pass in different object types each time you call the function. To call the function, simply reference it by name and pass in the desired arguments enclosed in () brackets. The code in the function block is then executed. You can use the return keyword to return a value from the function call.

The following code snippet shows examples of defining and calling functions: ...

Get jQuery and JavaScript Phrasebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.