Summary

There's an art to figuring out when to use a function and knowing the best way to write one. In general, the best time to use a function is for a simple task you need to execute more than once. For example, patching the Y2K bug in JavaScript is a task you may have to do repeatedly, so it's a good idea to create a function to handle it. As we see more complicated examples of JavaScript later in the book, you'll get a sense for what should go into functions. And, of course, as you view the source code on all the great web pages you see, you'll notice how various JavaScripters use functions.

Almost all complicated JavaScripts use at least one homemade function. In this chapter, you've seen how to write simple functions with no parameters and ...

Get The Book of JavaScript, 2nd Edition 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.