Latest web development tutorials
×

JavaScript Tutorial

JavaScript Tutorial JavaScript Introduction JavaScript usage JavaScript Output JavaScript grammar JavaScript Statement JavaScript Notes JavaScript variable JavaScript type of data JavaScript Object JavaScript function JavaScript Scope JavaScript event JavaScript The string JavaScript Operator JavaScript Comparison JavaScript If...Else JavaScript switch JavaScript for JavaScript while JavaScript Break & Continue JavaScript typeof JavaScript Type conversion JavaScript Regular Expressions JavaScript error JavaScript debugging JavaScript Variable promotion JavaScript Strict mode JavaScript Use errors JavaScript Form validation JavaScript Keep the keyword JavaScript JSON JavaScript void JavaScript Code specification

JS function

JavaScript Function definition JavaScript Function parameter JavaScript Function call JavaScript Closure

JS HTML DOM

DOM Introduction DOM HTML DOM CSS DOM event DOM EventListener DOM element

JS Advanced Tutorial

JavaScript Object JavaScript Number JavaScript String JavaScript Date JavaScript Array JavaScript Boolean JavaScript Math JavaScript RegExp Object

JS Browser BOM

JavaScript Window JavaScript Window Screen JavaScript Window Location JavaScript Window History JavaScript Navigator JavaScript Pop-ups JavaScript Timing events JavaScript Cookies

JS Library

JavaScript Library JavaScript test jQuery JavaScript test Prototype

JS Examples

JavaScript Examples JavaScript Object instance JavaScript Browser object instance JavaScript HTML DOM Examples JavaScript to sum up

JS Reference Manual

JavaScript Object HTML DOM Object

JavaScript function

Function is event-driven or reusable blocks of code executed when it is called.

Examples

<!DOCTYPE html>
<html>
<head>
<script>
function myFunction()
{
alert("Hello World!");
}

</script>
</head>

<body>
<button onclick=" myFunction() ">Try it</button>
</body>
</html>


JavaScript function syntax

Function block is wrapped in braces, and previously used the keyword function:

functionfunctionname()
{
执行代码
}

When the function is called, it will execute the code within the function.

It can directly call the function when an event occurs (such as when the user clicks the button), and JavaScript can be called at any position.

lamp JavaScript is case-sensitive. Keywords function must be lowercase, and must function with the same name as the case to call the function.


Function calls with arguments

When calling the function, you can pass it a value, these values ​​are called arguments.

These parameters can be used in the function.

You can send any number of parameters, separated by a comma (,):

myFunction(argument1,argument2)

When you declare the function, the parameters as variables declaration:

function myFunction( var1 , var2 )
{
代码
}

Variables and parameters must appear in the same order. The first variable is the first passed parameter given value, and so on.

Examples

<button onclick="myFunction( 'Harry Potter' , 'Wizard' )">Try it</button>

<script>
function myFunction( name , job )
{
alert("Welcome " + name + ", the " + job );
}
</script>

try it"

The above function button is clicked prompts "Welcome Harry Potter, the Wizard".

Function is very flexible, you can use different parameters to call this function, this will give a different message:

Examples

<button onclick="myFunction( 'Harry Potter' , 'Wizard' )">Try it</button>
<button onclick="myFunction( 'Bob' , 'Builder' )">Try it</button>

try it"

According to the different buttons you click, the above example will prompt "Welcome Harry Potter, the Wizard" or "Welcome Bob, the Builder".


Function returns a value with

Sometimes we want the function to return a value to the calling function.

By using the return statement can be achieved.

When using the return statement, the function stops execution and returns the specified value.

grammar

function myFunction()
{
    var x=5;
    return x;
}

The above function returns a value of 5.

Note: The entire JavaScript does not stop execution, just function. JavaScript code execution will continue from where the function is called.

Function calls will be returned value substituted:

var myVar=myFunction();

The value of myVar variable is 5, that is, "myFunction) (" the value returned by the function.

If not save it as a variable, you can use the return value:

document.getElementById("demo").innerHTML=myFunction();

innerHTML "demo" element will be 5, that is, "() myFunction" value returned by the function.

You can make the return value passed to a function based on the parameters:

Examples

The product of two numbers and returns the result:

function myFunction(a,b)
{
return a*b;
}

document.getElementById("demo").innerHTML=myFunction(4,3);

innerHTML "demo" element will be:

12

try it"

When you just want to exit the function, the return statement may also be used. The return value is optional:

function myFunction(a,b)
{
	if (a>b)
	{
		return;
	}
	x=a+b
}

If a is greater than b, then the above code will exit the function and does not calculate the sum of a and b.


Local JavaScript Variables

JavaScript variable declared inside a function (using var) is a local variable, so it can only be accessed within the function. (Scope of the variable is local).

You can use the same local variable name in different functions, because only declared the function of the variable to identify the variable.

As long as the function is completed, the local variable will be deleted.


Global JavaScript variable

Variables declared outside a function is a global variable that all scripts and functions on the page can access it.


Survival of JavaScript variables

Life of JavaScript variables from the time they are declared to start.

Local variables will be deleted after the function is run.

Global variables are deleted after the close of the page.


Assign values ​​to JavaScript variables undeclared

If you assign a value to a variable has not been declared, the variables will be automatically declared as global variables.

This statement:

carname="Volvo";

We will declare a global variable carname, even if it is performed within the function.