Best Website Builders CompanyBest Website Builders Company
    What's Hot

    Auto stocks in mild losses, Nifty Auto unchanged after RBI MPC keeps repo rate unchanged with eye on inflation

    June 9, 2023

    Realty stocks fall after RBI MPC keeps repo rate unchanged; Nifty Realty tumbles, DLF, Macrotech sink

    June 9, 2023

    What homebuyers should do now as RBI keeps repo rate unchanged

    June 8, 2023
    Facebook Twitter Instagram
    Facebook Twitter Instagram
    Best Website Builders CompanyBest Website Builders Company
    • Home
    • Web Builders
      1. Joomla
      2. WordPress
      3. CSS
      4. Web Design
      5. UX
      6. PHP
      7. View All

      For $50 you can host your website for life

      May 2, 2023

      California Department of Justice Investigating Shooting Involving CHP Officer in Glenn County Under AB 1506

      May 1, 2023

      Mariposa County Daily Sheriff and Reservation Report for Sunday, April 30, 2023

      May 1, 2023

      Top 10 Best Web Development Companies In India In 2023

      May 1, 2023

      Google Ads Sign Up – Easy Steps to Create Your Account

      May 17, 2023

      1Password puts users at ease after the horror of password change notifications

      May 3, 2023

      Samsung Galaxy S23 FE could feature a 50MP main camera, but we may have to wait until then

      May 3, 2023

      Titanfall director says Respawn is ‘looking forward to something new’

      May 3, 2023

      Implementing CSS with character and spirit: Union MoS Finance

      May 3, 2023

      Street Fighter 6’s unique character select screen animation really shows how much heart goes into the game

      May 3, 2023

      Make Google Chrome run faster with these 9 tips and tweaks

      May 3, 2023

      🅰️ New Angular 16 Goes Big in 2023: Everything You Need to Know | Vitaly Shevchuk | Oct 25, 2017 May 2023

      May 3, 2023

      18-Wheeler Accidents: Fatalities and Injuries

      May 6, 2023

      What Comes First in Website Development — Design or Copy?

      February 2, 2023

      Modern Campus Honors Best Higher Education Websites of 2022

      February 2, 2023

      Premier SEO Consultant in Las Vegas, Nevada with Unparalleled Customer Service

      February 2, 2023

      The role of artificial intelligence in improving the user experience in online casinos.

      May 3, 2023

      Microsoft enhances user experience with Windows 11 ‘smart opt-out’ and improved emergency notifications

      May 3, 2023

      Nigeria’s Nestcoin Launches New Digital Financial Platform For Africans

      May 3, 2023

      ibi WebFOCUS 9.2 is ready for Modern Business Intelligence, the Cloud, and Driving User Experience – PCR.

      May 3, 2023

      Anthony Carrigan Reflects on That ‘Barry’ Scene from Season 4 Episode 4

      May 1, 2023

      TikToker Kat Abu is very happy that Tucker Carlson has been fired

      April 28, 2023

      How ‘Single Drunk Female’ Season 2 Tackled Emotional Sobriety

      April 24, 2023

      Trans-Missouri Residents Affected by Attorney General Order

      April 24, 2023

      Creating and Adding a Google Account: A Step-by-Step Guide

      May 2, 2023

      How i Create New Google Account

      February 7, 2023

      CWT powers tools for meeting and event planners

      January 31, 2023

      Best Website Builder – Website Builders

      January 24, 2023
    • Realtoz
      • Our Other Sites
    • More News
    • Investments
    Best Website Builders CompanyBest Website Builders Company
    Home»JavaScript»Pure JavaScript functions and how to create them
    JavaScript

    Pure JavaScript functions and how to create them

    websitebuildersnowBy websitebuildersnowMarch 13, 2023No Comments5 Mins Read
    Facebook Twitter LinkedIn Telegram Pinterest Tumblr Reddit WhatsApp Email
    Share
    Facebook Twitter LinkedIn Pinterest Email


    A pure function is a function that produces no side effects and always returns the same output when called with the same input parameters.


    You can use pure functions to ensure your code is clean, maintainable and testable. These functions are perfect for these tasks because they are predictable and don’t change external state.

    It is also easier to debug, which helps in developing complex software systems. Here we discuss pure functions in JavaScript, their characteristics and advantages.


    Features of pure functions

    For a function to be “pure”, it must meet several requirements.

    Constant return value

    A pure function should always return the same value for the same input, no matter how many times it is called.

    For example, consider the function

     function multiply(a, b) {
      return a * b;
    }

    of times The function in the example above always returns the product of its two arguments. Given the same set of arguments, they have constant return values.

    Calling this function several times with the same arguments will produce the same output each time. for example:

     multiply(2, 3); 
    multiply(2, 3);
    multiply(2, 3);

    Or consider the following example.

     function multiplyRandomNumber(num) {
      return num * Math.floor(Math.random() * 10);
    }

    multiplyRandomNumber(5);
    multiplyRandomNumber(5);
    multiplyRandomNumber(5);

    of multiplication of random numbers The function above is impure because it returns a different result each time it is called. The results of this function are unpredictable, making it difficult to test components that rely on this function.

    no side effects

    A pure function must not have side effects. Side effects refer to changes in state or behavior outside the function’s scope, such as changes to global variables, console output, network requests, or DOM manipulation.

    If a pure function has side effects, it is no longer pure because it affects external state and violates the principle of no observable side effects. Thus, pure functions avoid side effects and avoid changing program state.

    For example, consider the following example.

     let count = 0;

    function increment() {
      count++;
      console.log(count);
    }

    increment();
    increment();
    increment();

    of Increment The function in this example has count Variables out of scope. It also logs to the console.

    This function has side effects and is not pure. This can make output difficult to predict and test in isolation.To make it pure, change it to count Takes a variable as an argument and returns the incremented value without changing the external state.

    It seems like:

     function increment(count) {
      return count + 1;
    }

    increment(1);
    increment(1);
    increment(1);

    version of Increment The function in the example above does not modify any external variables or record their values, so it has no side effects. Also, no matter how many times you call it, it will return the same value for the same input. So it’s a pure function.

    Other features

    When writing pure JavaScript functions, in addition to having a constant return value and producing no side effects, they should adhere to the following rules:

    • A function must not modify its arguments. Instead, make a copy of the argument and modify the copy if the operation requires modification.
    • A function must always have a return value. If a function has no return value or side effects, you can’t do anything!
    • Functions should not depend on external state.

    Advantages of pure functions

    Pure functions have some advantages over non-pure functions, some of which are:

    Testability

    Pure functions are easier to test because they have well-defined input and output behavior. Also, pure functions do not depend on external state or side effects. So you can test them independently without worrying about dependencies or interactions with other parts of your program.

    In contrast, testing impure functions that depend on external state or produce side effects can be more difficult. Because their behavior can depend on program state and other external factors. This can make it difficult to write comprehensive test cases and verify that your function behaves correctly in all scenarios.

    memoization

    Pure functions always produce the same output for the same input and have no side effects, so they are easy to memoize.

    By relying on these properties and using memoization, we can cache the result of a pure function call for a given input. The function can return cached results the next time it is called with the same inputs.

    Memoizing a pure function can improve program performance without worrying about interfering with program state, especially for expensive computations that repeatedly process the same input.

    In contrast, impure functions can produce different results for the same input, depending on program state or external factors. This makes memoization difficult because cached results may no longer be valid if the function’s dependencies or external state changes between calls.

    concurrent execution

    Pure functions are thread-safe because they do not modify external state or produce side effects. You can run them concurrently without worrying about race conditions or synchronization issues.

    In contrast, impure functions can be difficult to run in parallel because running them in parallel can interfere with each other or cause unexpected behavior. For example, two threads accessing and modifying the same global variable can overwrite each other’s modifications and produce inconsistent results.

    pure and impure functions

    You can write programs with a combination of pure and impure functions. This is because each type has a purpose.

    Pure functions are easier to optimize, test, and parallelize, making them suitable for use cases such as functional programming, caching, testing, parallel programming, and data processing tasks.

    However, impure functions pose challenges for testing and concurrency, but they are useful when working with mutable data structures or interacting with external systems and resources.



    Source link

    Share this:

    • Tweet
    • More
    • WhatsApp
    • Print
    • Share on Tumblr
    • Mastodon

    Related

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Telegram Email
    Previous ArticleNew Zealand v Sri Lanka: Kane Williamson century leads hosts to final-ball win
    Next Article Gary Lineker will abide by BBC guidelines until review completed – Tim Davie
    websitebuildersnow
    • Website

    Related Posts

    Fake ChatGPT extension to steal victim’s account details

    May 2, 2023

    What to expect from ECMAScript 2023 (ES14)

    May 2, 2023

    Which one is right for your project?

    May 2, 2023
    Add A Comment

    Leave a Reply Cancel reply

    Post Your Ad Free
    Advertisement
    Demo
    Top Posts

    Subscribe to Updates

    Get the latest sports news from SportsSite about soccer, football and tennis.

    This website provides information about CSS and other things. Keep Supporting Us With the Latest News and we Will Provide the Best Of Our To Makes You Updated All Around The World News. Keep Sporting US.

    Facebook Twitter Instagram Pinterest YouTube
    Top Insights

    Auto stocks in mild losses, Nifty Auto unchanged after RBI MPC keeps repo rate unchanged with eye on inflation

    June 9, 2023

    Realty stocks fall after RBI MPC keeps repo rate unchanged; Nifty Realty tumbles, DLF, Macrotech sink

    June 9, 2023

    What homebuyers should do now as RBI keeps repo rate unchanged

    June 8, 2023
    Get Informed

    Subscribe to Updates

    Get the latest creative news from FooBar about art, design and business.

    © 2023 bestwebsite-builders. Designed by bestwebsite-builders.
    • Home
    • About us
    • Contact us
    • DMCA
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.

    Go to mobile version
    x