In this article we will show you the solution of JavaScript function return value, a function is a self-contained block of statements that performs a coherent task. The return statement returns a value from a function.
Let us see the syntax of the JavaScript function:
function functionName ( formalArgument1 , formalArgument2 ) { statements return value } var a= ( ActualArgument1 , ActualArgument2 )
Step By Step Guide On JavaScript Function Return Value :-
Example 1
Firstly, let us see one simple example on function return value.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title> JavaScript function return value </title> </head> <body> <h1>TALKERSCODE</h1> <h3> JavaScript function return value </h3> <script> function Details( name , age ) { var a = name + " - " + age + " <br> "; return a; } var func = Details ( "Peter " , " 24 ") ; console.log(func) ; </script> </body> </html>
- First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
- Secondly, the <html> tag is used to indicate the beginning of an HTML document.
- As above now the <head> tag is used to contain information about the web page. In this tag, a <title> tag is used which helps us to specify a webpage title.
- Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
- Thirdly, the <body> tag is used to define the webpage body. All the contents to show on the website are written here.
- <h1> tag used to add heading here.
- we have created the<script> tag to add JavaScript.
- Create a function with some formal argument
- Create a variable then return the value
- Now create another variable with an actual argument
- Using console.log() to display the value.
Example 2
Let us see another example of a function return value. Here we will see a function to obtain the square of a number.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title> JavaScript function return value </title> </head> <body> <h1>TALKERSCODE</h1> <h3> JavaScript function return value </h3> <script> function square ( num ) { var y = num * num ; return ( y ) } var sq = square ( 4 ) ; console.log( sq ) ; </script> </body> </html>
- First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
- Secondly, the <html> tag is used to indicate the beginning of an HTML document.
- As above now the <head> tag is used to contain information about the web page. In this tag, a <title> tag is used which helps us to specify a webpage title.
- Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
- Thirdly, the <body> tag is used to define the webpage body. All the contents to show on the website are written here.
- <h1> tag used to add heading here.
- We have created the<script> tag to add JavaScript.
- Create a function named Square()
- Create a variable that is equal to the multiplication of the numbers then return the value of the square.
- Now create another variable with an actual argument
- Using console.log() to display the value.
Conclusion :-
Lastly here in conclusion, here we can say that with the help of this article we are able to function return value using JavaScript.
I hope this article on JavaScript function return value helps you and the steps and method mentioned above are easy to follow and implement.