Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint

Functions

Functions are pieces of code within your program that perform a specific task. By using functions, you are able to write reusable code, not only within the context of the program you are writing, but also with other programs that you may write. In JavaScript, a function consists of the keyword function, followed by the name of the function, a list of arguments, and the JavaScript statements.

The contents of a function (the function body) include some coding logic, and even calls to other functions. This is a way in which you can share bits of code throughout your program. However, on its own, a function is never executed. It needs to be called in the execution of your code. This is done by calling the function by its name. Take the following example which defines a function sayWhat() to log the word “what” to the console. It is called by executing the function in the code.


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial