What is the code that defines the function in javascript
This article mainly introduces "what is the code for defining functions in javascript". In daily operation, I believe that many people have doubts about what the code for defining functions in javascript is. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "what is the code for defining functions in javascript?" Next, please follow the editor to study!
Javascript defines the method of function: 1, use function keyword to define named function, syntax is "function function name (parameter) {code}"; 2, use "var x=function (name) {code};" define anonymous function.
The operating environment of this tutorial: windows10 system, javascript1.8.5 version, Dell G3 computer.
What is used to define functions in javascript
1. JavaScript uses the keyword function to define functions.
A function can be defined by a declaration or an expression.
Function declaration syntax:
Function functionName (parameters) {Code executed}
The function will not be executed immediately after it is declared, but will be called when we need it.
Example:
Function myFunction (a, b) {return a * b;}
Note:
Semicolons are used to separate executable JavaScript statements.
Because the function declaration is not an executable statement, it does not end with a semicolon.
2. Function expression
The JavaScript function can be defined by an expression.
Function expressions can be stored in variables:
Var x = function (a, b) {return a * b}
After a function expression is stored in a variable, the variable can also be used as a function:
Var x = function (a, b) {return a * b}; var z = x (4,3)
The above function is actually an anonymous function (the function has no name).
Functions are stored in variables, do not require a function name, and are usually called through the variable name.
Note: the above function ends with a semicolon because it is an execution statement.
Function () constructor
In the above example, we learned that the function is defined by the keyword function.
Functions can also be defined through the built-in JavaScript function constructor (Function ()).
Var myFunction = new Function ("a", "b", "return a * b"); var x = myFunction (4,3)
The above example can be written as follows:
Var myFunction = function (a, b) {return a * b}; var x = myFunction (4,3); at this point, the study of "what is the code that defines the function in javascript" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!