Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Javascript unexpected token in line 4 in custom small script

I am getting an error about:

Error: Parsing error: unexpected token in line 4 (bracket)

But if I delete this bracket the script will not work.
I am new to javascript so I would like somebody to please tell me what is wrong in this code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Javascript:

var game=document.querySelector("#fortuna"),
times=document.querySelector("nav");
game.addEventListener("click",(
function({currentTarget:e}){
let t=times.classList.toggle("aequilibrium");
e.textContent=t?"MENU":"CLOSE MENU"}));

>Solution :

Solution

var game = document.querySelector("#fortuna"); // need ";" instead of ","
var times = document.querySelector("nav");

// Use "event" instead of "{ currentTarget: e }"
game.addEventListener("click", function(event) {
  var t = times.classList.toggle("aequilibrium");
  event.currentTarget.textContent = t ? "MENU" : "CLOSE MENU";
});

or

var game = document.querySelector("#fortuna"); // need ";" instead of ","
var times = document.querySelector("nav");

// Use "{ currentTarget }" instead of "{ currentTarget: e }"
game.addEventListener("click", function({ currentTarget }) {
  var t = times.classList.toggle("aequilibrium");
  currentTarget.textContent = t ? "MENU" : "CLOSE MENU";
});

More information

Old
var statement – MDN Docs
I don’t recommend using var because var variables have functional scope, are globally or functionally scoped, and don’t have block scope, which can lead to confusing code and potential errors.

New
let statement – MDN Docs (recommended from ECMAScript 2015 (ES6))
const statement – MDN Docs (recommended from ECMAScript 2015 (ES6))

const game = document.querySelector("#fortuna"); // use "const"/"let" insteaf of "var" because you don't modified it in snippet
const times = document.querySelector("nav"); // use "const"/"let" insteaf of "var" because you don't modified it in snippet

game.addEventListener("click", function(event) {
  const t = times.classList.toggle("aequilibrium"); // use "const"/"let" insteaf of "var" - "const" because you don't modified it in snippet
  event.currentTarget.textContent = t ? "MENU" : "CLOSE MENU";
});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading