Day16
Functions in JavaScript
Functions of the function
By using a function declaration, followed by a set of parameters and the body of the function,
Grammar:
Functionfunctionname ([arg0, Arg1, argn]) {
Statements
}
Description
1.funtionName is the name of the function to be defined, which belongs to the identifier.
2.[] in the ARG0,ARG1,.. ARGN is the parameter of the function
3.[] Description inside the content is not necessary, it is not a grammar
Call to function
Grammar:
function name ([arg0, Arg1, ... argn])
return value of the function
Any function that returns a value through a return statement followed by a return value
Description
1. The function stops and exits immediately after executing the return statement.
The 2,return statement can also be used without any return value to stop the function prematurely
Execution does not require a return value.
Parameters in JavaScript
Arguments
The arguments in the ECMAScript are internally represented by an array, in the function body
This array parameter is accessed through the arguments object.
Description
1.arguments objects are just like arrays, not an instance of an array.
2.[] Syntax accesses every element of it.
The 3.length property determines the number of pass parameters.
function. HTML:
<!DOCTYPE HTML><HTMLLang= "en"><Head> <MetaCharSet= "UTF-8"> <title>Function</title></Head><Body> <Script> //declaring a function functionmyfun () {alert ("I am a function! "); } //Call to function //myfun (); //declaring a function with parameters functionAdd (num1,num2) {varsum=NUM1+num2; returnsum; } /*Console.log (Add (2,3)); Alert (Add (3,4)); document.write (Add (4,5));*/ functionMyFunction (ARG) {if(IsNaN (ARG))return; returnArg*2; } console.log (MyFunction ( +)); </Script></Body></HTML>
Arguments.html:
<!DOCTYPE HTML><HTMLLang= "en"><Head> <MetaCharSet= "UTF-8"> <title>Document</title></Head><Body> <Script> /*function inner () {Console.log (arguments[0]); Console.log (arguments.length);} Inner (3,4); function Add (num1,num2) {arguments[0] = 99; Console.log (NUM1); } add (33,44);*/ //An average of any set of numbers functionGetavg () {varsum= 0; for (varI= 0; I<arguments.length; I++) {sum+=Arguments[i]; } returnsum/arguments.length;} console.log (Getavg (4,5,6,2,3,4343,2)); </Script></Body></HTML>
03JavaScript function 16