In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you what the function in JavaScript is, I hope you have something to gain after reading this article, let's discuss it together!
What is the function in JavaScript?
A function is a block of code that encapsulates an independent, self-contained behavior for computer execution.
Functions are a set of organized instructions that correspond to specific tasks or functions that users want to implement in their programs to achieve a single desired result.
The code within the function runs only when needed, that is, when it is called.
Functions are an important and useful part of programming because they create reusable code.
Instead of copying, pasting, and repeating the same code in different parts of the program, you can use the function to write the code in only one place. You can then use it repeatedly as needed.
This is also helpful when you want to make changes or debug your program and try to fix errors.
Instead of looking for different parts of the code that might be located, you just need to look at a specific location that makes the code more readable.
How to declare a function in JavaScript
The general syntax for creating a function in JavaScript is as follows:
Function name (parameter1,parameter2,...) {/ / the code statements to be executed}
Let's break it down:
You declare a function with the function keyword.
Next, you specify a name of your choice for the function. Function names in JavaScript are case-sensitive, conventions and best practices are to use hump naming.
The function name is followed by a set of left and right parentheses.
Function can receive data through input. These inputs are enclosed in parentheses and are called parameters.
Parameters are used as local placeholder variables for values that are passed into the function as input when the function is called. They are completely optional, and if there are multiple, separate them with commas.
Finally, there are curly braces, inside which are the body of the function and the code statements to be executed when the function is called. This is where the function input is handled.
How to declare and call a simple function function greeting () {console.log ('Hello Worldwide') in JavaScript;}
Above, we created a function called greeting.
This function is a very basic function, and its function is very simple. It does not accept any input, and the only event that executes is to Hello World! Print to the console.
Defining a function itself does not run code inside the function itself. If we want to see the result of the run, we must call this function. This is also known as a function call.
To call a function that does not accept input, simply write the function name, followed by parentheses and semicolons.
Greeting (); / / output//Hello World!
Now, you only need to call the function itself multiple times to reuse the function multiple times. This helps avoid duplicating code:
Greeting (); / / output// Hello Worldhands / Hello Worldbacks / Hello World! How to declare and call functions with parameters in JavaScript
We can modify the previous example to get input. As mentioned earlier, we will do this using parameters.
Parameters are the values that you pass to the function when you declare it.
Function greeting (name) {console.log ('Hello' + name +'!');}
The named greeting function now accepts one parameter, name. Concatenate the hello string with name and the exclamation point at the end of the function using +.
When you call a function that accepts parameters, you need to pass in parameters.
Parameters are the values you provide when you call the function, and they correspond to the parameters passed in the declaration line of the function.
For example:
Greeting ('Jenny'); / / Output// Hello Jenny!
The parameter is the value Jenny, which you can think of as name = 'Jenny'. Name, the argument, is a placeholder variable, and Jenny is the value passed in when the function is called.
Function can accept multiple parameters or return data to the user of the program:
Function addNums (num1,num2) {return num1 + num2;}
The above code creates a function called addNums, which takes two parameters, num1 and num2, separated by commas.
In the same way that functions are input, they also output a value
This function returns the sum of num1 and num2 as its output. This means that it processes these two parameters, performs the calculation of the request, and returns the final value to the user as a result.
When you call a function, you must pass in two parameters because it accepts two parameters:
AddNums (10 and num2 20); / / Output// 30 and num2 / think of it as num1 = 10 camera = 20
Each time you call this function, you can pass in different parameters:
AddNums (2); / / 4addNums (3); / / the range of variables in the 18JavaScript function
The range of variables refers to the visibility of variables to different parts of the program.
Variables defined outside the function block and previously defined have a global scope and can be accessed from within the function:
Const num = 7 function myFunc () {console.log (num);} / / Access the variable with a global scope from anywhere in the program:console.log (num); / / Output//7//Call the function with the variable with global scopemyFunc (); / / Output//7
However, if the variable is defined within the function, it will have a local scope and will only be restricted and visible in the function in which it is defined.
You cannot access it from outside the function:
Function myFunc () {const num = 7; console.log (num);} / / Try to access the variable with local scope from outside the function scope:console.log (num); / / Otput://Uncaught ReferenceError: num is not defined//Call the function with the variable defined inside the function:myFunc (); / / Ouput//7 function expression
You can also use expressions to create functions.
These functions are created in expressions, not using function declarations as you have seen so far.
Const name = function (firstName) {return 'Hello' + firstName;}
Here, we use the variable name to store the function.
To call this function, you can use the variable name like this:
Console.log (name ('Jenny')); / / Output// "Hello Jenny"
Functions of this type are also called anonymous functions because they do not require a name.
The following lists the differences between named and anonymous functions:
/ / namedfunction name (firstName) {console.log ('Hello' + firstName);} name ('Jenny'); / / anonymousconst name = function (firstName) {return' Hello'+ firstName;} console.log (name ('Jenny'))
Variables in anonymous functions can also be used as values for other variables:
Const name = function (firstName) {return 'Hello' + firstName;} const myName = name ('Timmy'); console.log (myName); / / Ouput// "Hello Timmy" after reading this article, I believe you have some understanding of "what is the function in JavaScript". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.