GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.tutorialrepublic.com — Defining and Calling Functions in JavaScript - Tutorial Republic JAVASCRIPT BASIC
JS Introduction
JS Getting Started
JS Syntax
JS Variables
JS Generating Output
JS Data Types
JS Operators
JS Events
JS Strings
JS Numbers
JS If…Else
JS Switch…Case
JS Arrays
JS Sorting Arrays
JS Loops
JS Functions
JS Objects
JAVASCRIPT & DOM
JS DOM Nodes
JS DOM Selectors
JS DOM Styling
JS DOM Get Set Attributes
JS DOM Manipulation
JS DOM Navigation
JAVASCRIPT & BOM
JAVASCRIPT ADVANCED
JS Date and Time
JS Math Operations
JS Type Conversions
JS Event Listeners
JS Event Propagation
JS Borrowing Methods
JS Hoisting Behavior
JS Closures
JS Strict Mode
JS JSON Parsing
JS Error Handling
JS Regular Expressions
JS Form Validation
JS Cookies
JS AJAX Requests
JS ES6 Features
JAVASCRIPT EXAMPLES
JAVASCRIPT REFERENCE
Advertisements
JavaScript FunctionsIn this tutorial you will learn how to define and call a function in JavaScript. What is Function?A function is a group of statements that perform specific tasks and can be kept and maintained separately form main program. Functions provide a way to create reusable code packages which are more portable and easier to debug. Here are some advantages of using functions:
The following section will show you how to define and call functions in your scripts. Defining and Calling a FunctionThe declaration of a function start with the
function functionName() {
// Code to be executed } Here is a simple example of a function, that will show a hello message: ExampleTry this code »
Once a function is defined it can be called (invoked) from anywhere in the document, by typing its name followed by a set of parentheses, like Note: A function name must start with a letter or underscore character not with a number, optionally followed by the more letters, numbers, or underscore characters. Function names are case sensitive, just like variable names. Adding Parameters to FunctionsYou can specify parameters when you define your function to accept input values at run time. The parameters work like placeholder variables within a function; they're replaced at run time by the values (known as argument) provided to the function at the time of invocation. Parameters are set on the first line of the function inside the set of parentheses, like this:
function functionName(parameter1, parameter2, parameter3) {
// Code to be executed } The ExampleTry this code »
You can define as many parameters as you like. However for each parameter you specify, a corresponding argument needs to be passed to the function when it is called, otherwise its value becomes ExampleTry this code »
Default Values for Function Parameters ES6With ES6, now you can specify default values to the function parameters. This means that if no arguments are provided to function when it is called these default parameters values will be used. This is one of the most awaited features in JavaScript. Here's an example: ExampleTry this code »
While prior to ES6, to achieve the same we had to write something like this: ExampleTry this code »
To learn about other ES6 features, please check out the JavaScript ES6 features chapter. Returning Values from a FunctionA function can return a value back to the script that called the function as a result using the The ExampleTry this code »
A function can not return multiple values. However, you can obtain similar results by returning an array of values, as demonstrated in the following example. ExampleTry this code »
Working with Function ExpressionsThe syntax that we've used before to create functions is called function declaration. There is another syntax for creating a function that is called a function expression. ExampleTry this code »
Once function expression has been stored in a variable, the variable can be used as a function: ExampleTry this code »
Note: There is no need to put a semicolon after the closing curly bracket in a function declaration. But function expressions, on the other hand, should always end with a semicolon. Tip: In JavaScript functions can be stored in variables, passed into other functions as arguments, passed out of functions as return values, and constructed at run-time. The syntax of the function declaration and function expression looks very similar, but they differ in the way they are evaluated, check out the following example: ExampleTry this code »
As you can see in the above example, the function expression threw an exception when it was invoked before it is defined, but the function declaration executed successfully. JavaScript parse declaration function before the program executes. Therefore, it doesn't matter if the program invokes the function before it is defined because JavaScript has hoisted the function to the top of the current scope behind the scenes. The function expression is not evaluated until it is assigned to a variable; therefore, it is still undefined when invoked. ES6 has introduced even shorter syntax for writing function expression which is called arrow function, please check out the JavaScript ES6 features chapter to learn more about it. Understanding the Variable ScopeHowever, you can declare the variables anywhere in JavaScript. But, the location of the declaration determines the extent of a variable's availability within the JavaScript program i.e. where the variable can be used or accessed. This accessibility is known as variable scope. By default, variables declared within a function have local scope that means they cannot be viewed or manipulated from outside of that function, as shown in the example below: ExampleTry this code »
However, any variables declared in a program outside of a function has global scope i.e. it will be available to all script, whether that script is inside a function or outside. Here's an example: ExampleTry this code »
Advertisements
Advertisements
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |