"return" Statement and Return Value

This section provides a quick description on 'return' statements and return values. A tutorial example on how return values are passed back to calling expressions is provided.

JavaScript supports "return" statements to allow functions to return values back to calling expressions. Here are some basic rules on the return value from a function.

To help you understand different types of return values, I wrote this JavaScript tutorial example:

<!-- Return_Values.html
   Copyright (c) 2013 by Dr. Herong Yang, herongyang.com
<head><title>Return Values</title></head>
<script type="text/javascript">

   var resValue = valueReturn();
   var resObject = objectReturn();
   var resUndefined = noReturn();

   document.write("\n\nAfter function calls:");
   document.write("\n   resValue = " + resValue);
   document.write("\n   resObject = " + resObject[0]);
   document.write("\n   resUndefined = " + resUndefined);

function valueReturn() {
   document.write("\nInside valueReturn()");
   var value = "String value";
   return value;

function objectReturn() {
   document.write("\nInside objectReturn()");
   var object = new Array("Array object");
   return object;

function noReturn() {
   document.write("\nInside noReturn()");

The output of this tutorial example shows no surprises:

Inside valueReturn()
Inside objectReturn()
Inside noReturn()

After function calls:
   resValue = String value
   resObject = Array object
   resUndefined = undefined

Table of Contents

 About This Book

 Introduction to JavaScript

 ECMAScript Language Specification and JavaScript Dialects

 Data Types, Variables and Expressions

 Flow Control Statements

 Creating, Accessing, and Manipulating Arrays

Defining and Calling Functions

 Defining Your Own Functions

 Defining Your Own Functions - Example

 Calling Your Own Functions - Example

 Passing Parameters by Value or by Reference

 Function Parameters Are Passed as Local Copies

 Function Parameters Are Passed as Local Copies - Example

 Global and Local Variables - Scope Rules

 Global Variables - Examples

 Local Variables - Examples

 Collision of Global and Local Variables - Examples

"return" Statement and Return Value

 Web Browser Supporting JavaScript

 Server-Side and Client-Side Web Scripting

 Introduction to Objects

 Defining Your Own Object Types

 Inheritance of Properties and Methods through the Prototype Object Chain

 'jrunscript' - JavaScript Shell Command from JDK

 Using Functions as "Function" Objects

 Introduction to Built-in Object Types

 W3C's Document Object Model (DOM) Specifications


 PDF Printing Version