Chapter 12

Making It Functional

Functions are the building blocks of JavaScript programs. They help you to keep from having to repeat yourself, and they make your programs neater and more flexible!

In this chapter, we use functions to create a game called Function Junction.

image

Understanding Functions

Functions are programs inside of programs. Functions are great at handling tasks that may be required multiple times by different parts of a program. They’re also a great way to keep things organized within your program.

Built-in functions

Some functions are built into JavaScript, such as the methods of arrays, strings, numbers, and other objects. You can tell that something’s a function because it has parentheses after it.

When a function is part of an object (such as the document object, for example) we call it a method. But it’s still a function.

Here are some of the functions that are built into JavaScript that you’ve already used:

getElementById()

toString()

addEventListener()

indexOf()

Can you think of other functions you’ve seen?

Custom functions

In addition to the functions that are built into JavaScript, you can also create your own, using the steps and information that we’re about to show you!

remember You can’t spell function without “fun,” and you can’t write JavaScript ...

Get JavaScript For Kids For Dummies now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.