Getting Information from Functions

You can also write functions that give information back to you. Consider the prompt() function:

var the_answer = prompt("What's your name?","Ishmael");

When a user types his or her name into the prompt box and clicks OK, the name goes into the variable the_answer. In programming parlance, you'd say that the function prompt() returns the words typed into the prompt box. The functions you write can return values as well. Figure 6-9 shows a very simple example of how to make a function return values.

A script with a simple function that returns a value

Figure 6-9. A script with a simple function that returns a value

Line-by-Line Analysis of Figure 6-9

Most of the function ...

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.