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:









4 comments:

  1. I in addition to my friends have already been studying the good key points located on your web site and immediately got a terrible feeling I never thanked the blog owner for those tips on . HOW TO OBTAIN A VALID REGISTERED PASSPORT ONLINE THAT WORKS
    Y’all don’t forget to join this TELEGRAM GROUP for more information about a passport from any country. You can also take advantage to learn and meet many Experts who will guide you on numerous techniques for anyone who love hacking and don’t know how to go about it .

    ReplyDelete
  2. 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
  3. The Assignment Writing Service In Manchester is working highly professional in display of most strong functionality

    ReplyDelete
  4. They are sometimes highly custom-made, with distinctive designs that make them suitable for very specific applications associated Hand Massagers to human anatomy. Primarily they must be secure to make use of, and that diploma of security of course includes the uncooked supplies from which they’re made. Quality craftsmanship made possible by the very best high-precision CNC machines and trained machinists around.

    ReplyDelete

Search This Blog