Home > Web Front-end > JS Tutorial > Learn function calls and return values ​​in JavaScript

Learn function calls and return values ​​in JavaScript

WBOY
Release: 2023-11-03 18:24:19
Original
1293 people have browsed it

Learn function calls and return values ​​in JavaScript

To learn function calls and return values ​​in JavaScript, you need specific code examples

In JavaScript, a function is a reusable block of code. By calling functions, we can perform a series of operations in the program. Function calls and return values ​​are one of the very important concepts in functional programming. In this article, we will learn how to call a function and how to use the return value.

1. Function Calling
In JavaScript, we can call functions in the following two ways:

1. Directly call the function name
Example:

function sayHello() {
    console.log("Hello!");
}

sayHello(); // 调用函数
Copy after login

In the above code, we have defined a function called sayHello, we can call the function directly by adding brackets after the function name and passing the parameters in the brackets (if any) . In this example, the sayHello function is passed no parameters and only outputs a simple message.

2. Use function expressions
Example:

const sayGoodbye = function() {
    console.log("Goodbye!");
}

sayGoodbye(); //调用函数
Copy after login

In the above code, we use function expressions to define a function named sayGoodbye. Similar to calling the function name directly, we can also call the function by adding parentheses after the function expression.

2. Function return value
In JavaScript, a function can return a value. By using the return keyword, we can return a value from a function to the caller of the function.

Example:

function addNumbers(num1, num2) {
    return num1 + num2;
}

const result = addNumbers(5, 10);
console.log(result); // 输出15
Copy after login

In the above code, we define a function named addNumbers, which accepts two parameters num1 and num2. By adding these two parameters and returning the result using the return statement, we can obtain the result of the calculation when calling the function.

It should be noted that once the function executes the return statement, it will immediately stop execution and return the return value to the caller of the function. Therefore, the return statement usually appears on the last line of a function. If you use multiple return statements in a function, only the first return statement to be executed will take effect.

In addition, if the function does not specify a return value, or does not use the return statement, the function will return undefined by default.

Summary:
By studying this article, we understand how to call functions in JavaScript and how to use the return value of the function. We can execute a specific block of function code by calling the function name directly or using a function expression. By using the return keyword, we can return the calculation result from the function to the caller of the function. This allows us to use the result of the function for subsequent operations in the program. In future studies, we will also learn more about more function-related concepts and usage.

The above is the detailed content of Learn function calls and return values ​​in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

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