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

What are the keywords used to define methods in javascript

藏色散人
Release: 2023-01-05 16:14:21
Original
6364 people have browsed it

The keyword for defining methods in JavaScript is function; the syntax for using the function keyword is such as "function functionName(parameters) {executed code}".

What are the keywords used to define methods in javascript

The operating environment of this article: windows7 system, javascript version 1.8.5, Dell G3 computer.

JavaScript function definition

JavaScript uses the keyword function to define functions.

A function can be defined through a declaration or an expression.

Function declaration

In the previous tutorial, you have already understood the syntax of function declaration:

function functionName(parameters) {
  执行的代码
}
Copy after login

The function will not be executed immediately after it is declared, but will be executed when we need it Called to.

Example

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title></title> 
</head>
<body>

<p>本例调用的函数会执行一个计算,然后返回结果:</p>
<p id="demo"></p>
<script>
function myFunction(a,b){
	return a*b;
}
document.getElementById("demo").innerHTML=myFunction(4,3);
</script>

</body>
</html>
Copy after login

Output:

本例调用的函数会执行一个计算,然后返回结果:
12
Copy after login

Recommended study: "javascript Advanced Tutorial"

The above is the detailed content of What are the keywords used to define methods in javascript. 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!