Home > Web Front-end > JS Tutorial > body text

what is javascript function

藏色散人
Release: 2023-01-04 09:35:50
Original
3918 people have browsed it

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.

what is javascript function

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, ...)
Copy after login

Code executed by a function is placed within curly braces: {}

function name(参数 1, 参数 2, 参数 3) {
    要执行的代码
}
Copy after login

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 的乘积
}
Copy after login

x The result will be:

56
Copy after login

[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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!