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

Thursday, December 9, 2021

JavaScript Functions: Storing Functions As Values

JavaScript is a flexible language, and that flexibility extends to functions.  A JavaScript function are not just codes that you reuse and execute.  There are also values, values that you assign to a variable.  In the following example we are going to write a function that will calculate the radius of a circle.  But instead of invoking radius function like a normal function call, we are going to assign to a function to a variable first.


The following is the formula for a radius according to Google












Here is the code to calculate the radius of a circle in JavaScript


    <script>

        var r = radius;

        function radius(circumference) {
            return circumference / (2 * Math.PI);
        }

        console.log(r(10));

    </script>

The code above returns a value of the radius by dividing the circumference by 2*Math.PI and outputting the result to the console log.  Notice once we assign the function to the radius to variable r, we can use the variable r in place of the function radius.

Here is the result:









6 comments:

  1. The javascript use these command line most widely amongs the user , in similar topic i've buy essay uk service in past few days where I specified the reasons behind that

    ReplyDelete
  2. The Assignment Writing Service In Manchester is working highly professional in display of most strong functionality

    ReplyDelete
  3. The Surah Nas PDF version is a great way to gain a deeper understanding of the Surah Nas and its spiritual power. It is a great tool for those looking to gain a better understanding of Islam and its teachings.

    ReplyDelete
  4. Get ready to level up your style game with the andrew tate leather jacket in the USA! Inspired by the renowned athlete, this jacket is the epitome of coolness.

    ReplyDelete
  5. In the USA, Mens Leather Shirt are a fashionable choice for those seeking a unique and edgy look. Available in various styles, such as button-up or pullover, leather shirts offer a combination of comfort and style. Crafted from high-quality leather, these shirts provide a rugged yet sophisticated appeal for the modern man.

    ReplyDelete
  6. Wear the Roar England Lionesses Puffer Jacket It's more than just a jacket it's a statement.

    ReplyDelete

Search This Blog