Skip to main content

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.

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

    Code for solution

    const myFunction = (val) => { return 'You have pass input to function = '+ val  }
    const output = myFunction(20);
    console.log(output);

    Code snippet which produce error - ReferenceError: Cannot access 'myFunction' before initialization

    const output = myFunction(20);
    console.log(output);
    const myFunction = (val) => { return 'You have pass input to function = '+ val  }

    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