Home > Article > Web Front-end > what is javascript function
Javascript function is a block of code designed to perform a specific task. A JavaScript function will be executed when a certain code calls it; a JavaScript function is defined by the function keyword, followed by the function name and parentheses (), function The name can contain letters, numbers, underscores, and dollar signs.
The operating environment of this article: Windows 7 system, JavaScript version 1.8.5, DELL G3 computer.
JavaScript functions are blocks of code designed to perform specific tasks.
JavaScript functions are executed when some code calls it.
JavaScript function syntax
JavaScript functions are defined through the function keyword, followed by the function name and brackets ().
Function names can contain letters, numbers, underscores, and dollar signs (the rules are the same as variable names).
Parentheses can include parameters separated by commas:
(参数 1, 参数 2, ...)
Code executed by a function is placed within curly braces: {}
function name(参数 1, 参数 2, 参数 3) { 要执行的代码 }
Function parameters (Function parameters) is the name listed in the function definition.
Function arguments are the actual values received by the function when it is called.
In functions, parameters are local variables.
In other programming languages, functions approximate procedures (Procedure) or subroutines (Subroutine).
Function Calls
The code in the function will be executed when other code calls the function:
When the event occurs (When the user clicks the button)
When the JavaScript code is called
Automatically (self-calling)
You will learn more about function calls in this tutorial.
Function return
When JavaScript reaches the return statement, the function will stop executing.
If a function is called by a statement, JavaScript will "return" the execution code after the calling statement.
Function usually calculates the return value. This return value will be returned to the caller:
Instance
Calculate the product of two numbers and return the result:
var x = myFunction(7, 8); // 调用函数,返回值被赋值给 x function myFunction(a, b) { return a * b; // 函数返回 a 和 b 的乘积 }
x The result will be:
56
[Recommended learning: javascript advanced tutorial]
The above is the detailed content of what is javascript function. For more information, please follow other related articles on the PHP Chinese website!