ReferenceError: Cannot access 'myFunction' before initialization

Error ReferenceError: Cannot access 'myFunction' before initialization occurs in javascript when you call function before initializing it. You have to create function above in the page before the function call.

Answers 1
  • Solution for error ReferenceError: Cannot access 'myFunction' before initialization in javascript

    <!-- Code for solution -->
    <script>
    const myFunction = (val) => { return 'You have pass input to function = '+ val  }
    const output = myFunction(20);
    console.log(output);
    </script>
    <!-- Code snippet which produce error - ReferenceError: Cannot access 'myFunction' before initialization -->
    <script>
    const output = myFunction(20);
    console.log(output);
    const myFunction = (val) => { return 'You have pass input to function = '+ val  }
    </script>
    
    0

    You have to define function (above) first before calling it using const in javascript to avoid error ReferenceError: Cannot access 'myFunction' before initialization.

  • Back to code snippet queries related javascript