Getting an error as 'button is not defined.'

Hello team, I am working on js functions and have got the below error, how to resolve this? Thank you.

this is the link to my code.

Hello vaishnavi,

In this line button.addEventListener('click',displayMessage); you’re trying to add an event listener to the button element stored in the button constant.

But that constant is declared inside the local scope of the displayMessage function. Because of that is not available outside and it results in a not defined error.

Try to move outside the function the const button = document.querySelector('button'); declaration and the problem should be resolved.

2 Likes

oh yes, i was continuously checking where did I go wrong, Thank u very much @Manuel_Ricci.
Your reply is super useful.
:+1: :+1:

1 Like