Creating Function Objects with "function" Statements

This section provides a tutorial example on how to create a Function object with a 'function' statement, which also assigns the object to a variable.

By definition, a function is an object instance of the Function object type. In previous sections, we learned how to create a function with the Function constructor.

A function can also be created with the "function" statement:

function function_name(parameter_1, parameter_2, ...) {
   return return_expression

The "function" statement actually does 4 things:

The tutorial example below shows you how to use a "function" statement to create a Function object:

// Function_Statements.js
// Copyright (c) 2013 by Dr. Herong Yang,

// Using the "function" statement
function f2c(fahrenheit) {
   println("Converting Fahrenheit = "+fahrenheit);
   var celsius = (fahrenheit - 32.0 ) / 1.8;
   println("Returning Celsius = "+celsius);
   return celsius;

   println("Function body: " + f2c.toString());

   println("# of parameters: " + f2c.length);

   var received =, 70.0);
   println("Received Celsius = "+received);

Run this JavaScript file with "jrunscript" in a command window, you will get:

# of parameters: 1
Converting Fahrenheit = 70
Returning Celsius = 21.11111111111111
Received Celsius = 21.11111111111111

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

 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

 Functions Are Objects of the "Function" Type

 Using the Function Constructor

 Function Object Inherited Properties and Methods

 Function Object Instance Properties

Creating Function Objects with "function" Statements

 Creating Function Objects with the "function" Operator

 Comparing 3 Ways of Creating Functions

 Introduction to Built-in Object Types

 W3C's Document Object Model (DOM) Specifications


 PDF Printing Version