The return Statement

The return statement is discussed here instead of in the last chapter because the statement can be used only as part of a function. The role of the return statement is to provide the value of the expressions within the function. But where does the function return the value to? For example, try out the following script:

<html> 
<head> 
<title>Return</title> 
<script language="JavaScript"> 
function returnMessage() {
      var part1="Good "; 
      var part2="Morning"; 
      var wholeThing=part1 + part2; 
      return wholeThing; 
      } 
returnMessage(); 
</script> 
</head> 
<body bgcolor="mistyrose"> 
</html> 

When you run the previous program, nothing appears on your screen other than having a nice, misty, rose-colored screen. The string value “Good Morning” ...

Get JavaScript Design 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.