All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

JavaScript Function Return Value

Last Updated : Mar 11, 2024

JavaScript Function Return Value

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>
  1. First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. 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.
  4. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  5. Thirdly, the <body> tag is used to define the webpage body. All the contents to show on the website are written here.
  6. <h1> tag used to add heading here.
  7. we have created the<script> tag to add JavaScript.
  8. Create a function with some formal argument
  9. Create a variable then return the value
  10. Now create another variable with an actual argument
  11. 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>
  1. First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. 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.
  4. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  5. Thirdly, the <body> tag is used to define the webpage body. All the contents to show on the website are written here.
  6. <h1> tag used to add heading here.
  7. We have created the<script> tag to add JavaScript.
  8. Create a function named Square()
  9. Create a variable that is equal to the multiplication of the numbers then return the value of the square.
  10. Now create another variable with an actual argument
  11. 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.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪