JavaScript Return Command20 Apr 2025 | 4 min read This article gives an overview of the return statement in JavaScript. The return statement is placed after the execution of a function and is used to stop any further processing within that function while returning a specified value. In JavaScript, it is necessary for a function to explicitly include a return statement to convey the value of the expression contained within it. Further, when the return statement is not specified in the code, the compiler or interpreter automatically sends back a value from the function. The common case for this returned value by the function is it goes back to the one that invoked it. Functions can return values in two forms:
Syntax In this context, (expression) is:
Automatic Semicolon Insertion (ASI)This would follow the rule of Automatic Semicolon Insertion, or ASI. This implies that no line terminator is present between the return statement and the corresponding expression to which it may refer. But if the return statement is declared in this way: If the return statement is expressed in the following manner: It can be reformulated by Automatic Semicolon Insertion (ASI) as: This will print a warning that indicates that there is an unreachable code after the return statement. With this, to solve the problem and avoid ASI, the expression that follows the return must be in the parentheses: Functionality of the return statement in JavaScript with illustrationsThis article will explore how return works in JavaScript. A return statement is mostly used inside a function to provide a value while ending the execution of that function. By default, an expression that doesn't evaluate to a value returns undefined. Here is a simple example of how a return statement is used in JavaScript: Example: Define a function that computes and returns the area of the triangle. Code Output ![]() Example 1 Below, we will examine a straightforward illustration of a return statement in JavaScript. Code Output ![]() In the program mentioned above, there is a function named "myFunction" that takes in two arguments and returns their sum. This function is defined so that it will accept any numerical pair of input values, thus returning the result of their sum. The return statement includes the expression "a1 + a2," which performs the addition. Furthermore, the result from this computation is assigned to a variable called "result" using the assignment operator ("="). The value of the expression is then stored in the variable, which is then printed out every time the function is called. Next we will look at an example of what occurs should we remove the return statement altogether. In this case, we need to be sure that we explicitly return the result of the expression; otherwise, the interpreter will just print out an undefined output from the function. Example 2 Code Output ![]() In the function shown above, there is no return statement during the computation of a number's square times its product since the declared function is template. It thus means that no return keyword would be placed before the expression and the value of that expression will therefore be printed "undefined". Consequently, the square of a number will print out "undefined" instead of giving the correct square value of that number. This paper had explained the use of the return statement in JavaScript as follows: Here is an example of a function that demonstrates the use of return to get out of the loop. Example 3 Code Output ![]() Above, the function "example" is defined that has a for loop and it is anticipated to print some values. Still, the return statement in the function will end the loop at the finding of the value of 5 though the loop can run up to 10. ConclusionIn the section above, it is shown that the return statement in JavaScript has several different functions.
Next TopicConvert Values to Number JavaScript |
The regex "\v" shows the available vertical tab character in the input string in JavaScript. If the vertical tab element is available, then the position of the values shows; otherwise, the "-1" value shows using the search method. We can test, search, and match the...
9 min read
An overview of insertion sort in JavaScript Sorting is a concept that must be understood by anyone going on a journey in computer science, regardless of the programming language they choose to study. The process of sorting allows us to find the data we want to retrieve...
6 min read
A bundler is a development tool that creates a single, production-ready JavaScript code file. It can loaded in a browser from many files and operate hustle-free functionality. A bundler's ability to make a dependency graph to navigate the first code files is a great feature. This...
9 min read
Calendars are a critical component of any web or mobile application you design. Manually adding these calendars can take a lot of effort and time. Because of this, there are many JavaScript calendar libraries available that come in handy if you want to include multifunctional calendars...
14 min read
A comma-separated valuation (CSV) file is a demarcated file format that uses a comma to isolate the values. The data record is comprised of one or more than one field, separated by the commas. The name root for this file format is the comma-separated file...
5 min read
A web-based platform called a JavaScript playground enables users to write, test, and experiment with JavaScript code in a convenient and interactive setting. Code editors, console outputs, and occasionally extra debugging and visualisation tools are among the capabilities that these playgrounds usually include. An overview...
3 min read
The nullish coalescing operator (??) is a simple yet powerful linguistic addition to JavaScript that makes dealing with those null and undefined values a simple matter. The nullish coalescing operator helps with configuration objects, API responses, and optional parameters, whether you're even working with it or not....
6 min read
A relatively simple data-interchange format known as JSON (JavaScript Object Notation) is essential to contemporary web development. It makes data structuring simpler and more readable for humans, which makes it suited for information transfer between a client and a server. Because of its ease of use...
7 min read
How to Run a JavaScript Program in Notepad JavaScript is a flexible programming language that is mainly used for online development, but it can also be used with simple programs like Notepad. Even though advanced Integrated Development Environments (IDEs) are readily available, learning the foundations of...
6 min read
We will understand the in this article. How to define and access an object in JavaScript? We can define the JavaScript Object in the following ways: let keyword const keyword Utilizing let keyword We will utilize the let keyword and create the object using curly braces. Creating the object using the...
3 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India