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:









8 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
  7. The Garage Equipment Association (GEA) in the UK and Europe proudly embraces a commitment to environmental sustainability with its RoHS-compliant practices. By adhering to the Restriction of Hazardous Substances (RoHS) regulations, GEA ensures that its members prioritize the use of electronic equipment free from harmful substances. This dedication not only aligns with legal obligations but also underscores GEA's proactive role in fostering eco-friendly practices within the automotive industry. ROHS compliant by GEA contributes to a cleaner, safer, and more environmentally responsible automotive service sector, promoting the well-being of both industry professionals and the broader community.

    ReplyDelete
  8. In today's technology-driven world, software application developer, play a pivotal role in shaping our digital landscape. These skilled professionals bring ideas to life, creating applications that streamline processes, entertain, inform, and solve problems. From mobile apps to complex software systems, their work is instrumental in our daily lives.

    ReplyDelete

Search This Blog