Js help, Uncaught SyntaxError: Unexpected token '{'

Hi everyone, I’m learning to use javascript and I ran into this problem.
Can someone help me? I am pretty sure the syntax is correct but I keep getting this error: Uncaught SyntaxError: Unexpected token '{'.
p.s I apologize in advance for my not so great english :sweat_smile:error

Putting an opening brace ({) on the same line as your call to the openMenu function is causing the problem. Separating code blocks with braces is not commonly used in JavaScript, but you can do it. You’ll just want a semicolon (;) after openMenu() and to put that brace on its own line for readability.

Hi Ariel, I tried as you say but it doesn’t work anyway because when I go to put the semicolon after openMenu() the error becomes Uncaught ReferenceError: openMenu is not defined.
Am I not breaking the function definition if I put the semicolon after openMenu()?

Well, that would happen if you haven’t defined the function openMenu anywhere.

1 Like
  1. define the function
function hello() { console.log("hello, friend");
  1. assign the function to the click property
button.onclick = hello;
  1. now, when you click:
//hello, friend

Some variations:
anonymous function

button.onclick = function() {console.log("hello, friend")}

event-listener (I only use this one)

button.addEventListener("click", hello);
//or
button.addEventListener("click", function() {console.log("hello, friend")})
3 Likes