O'Reilly logo

jQuery and JavaScript Phrasebook by Brad Dayley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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: ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required