Tech Junkie Blog - Real World Tutorials, Happy Coding!: JavaScript Functions

Friday, December 17, 2021

JavaScript Functions

A JavaScript function is a piece of code that is defined once and can be reused through your program.  A function can have arguments or parameters that you can pass into it and returns a value.  If an explicit return value is not specified the return value is "undefined".  During invocation of the function, the function returns the value of "this" keyword, which represents the context of the function.

As you can see a function is a lot like a method.  If a function is defined as a property of an object it is in fact called a method of the object.  It is import to keep in mind that functions in JavaScript are objects and they have the benefits of any objects, meaning they can be self contained, which provide them with a vast amount of flexibility.



The following code is a simple function that adds two numbers together


            
<script>
        function addNumbers(x, y) {
            return x + y;
        }

        console.log("The sum of 1 + 3 is " + addNumbers(1, 3));
</script>






The code above creates a function called addNumbers with two parameters and returns the sum of the two numbers


2 comments:


  1. It is shockingly simple to find 3 BHK pads and 2 BHK pads in Noida Extension and are accessible at reasonable rates.visit:- Ace Divino

    ReplyDelete
  2. I appreciate you sharing this blog. It's a fantastic blog, and I'm extremely impressed by how valuable it is. Are you aware? How are dice rolled on a d4? If not, learn more about it by looking at the d4 dice online blog. This blog guide you more.

    ReplyDelete

Search This Blog