Unexpected token , syntax


Please in the picture above there is a typo
Error and I can’t figure out what my mistake is please help me
Thank you

Please post your actual code instead of a picture. Thank you


let list =document.getElementById("list")
let arr=[]
let nameValue =""
let storage
 =JSON.parse(localStorage.getItem("lists"))
let checkBoxes= []

//console.log(checkBoxes)

//console.log(storage)
if (storage){ render(storage)}
 else {storage=[]}

function reload(){

    window.location.href = window.location.href;

}
function render(array){
           /*   for (let i=0; i<array.length; i++)*/
  array.forEach(function (el){list.innerHTML+='<input type="checkbox">' + el ;})}

function saveState(){
 checkBoxes = document.querySelectorAll('input[type="checkbox"]')
console.log(checkBoxes)
checkBoxes.forEach(function (el){currentState=el.checked})
}

function save (){ nameValue =document.getElementById("name").value
//console.log(nameValue);
  let contentValue =document.getElementById("content").value
 //console.log(contentValue);
                arr = contentValue.split(",")
   arr.forEach(function(el){storage.push(el);
   console.log (storage)}) localStorage.setItem("lists",JSON.stringify(storage))
  render(arr);
          content.value=""}
           function deleter() {      localStorage.clear()
                   reload()}
                           //   console.log (localStorage)}

I would recommend you format your code a little nicer and always use semi-colons at the end of every statement. This might help you see the error a little easier and prevent you making them in the first place.

1 Like

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

1 Like

unexpected token almost always means that you forgot to close something: quotes, brackets, parentheses, etc.

You’re at a higher risk of this error if you choose not to use semicolons at the end of all commands, because sometimes they are required.

Nobody can read code written like that, and in this case, neither can the parser/interpreter apparently.

let list = document.getElementById('list');
let arr = [];
let nameValue = '';
let storage = JSON.parse(localStorage.getItem('lists'));
let checkBoxes = [];

//console.log(checkBoxes)

//console.log(storage)
if (storage) {
  render(storage);
} else {
  storage = [];
}

function reload() {
  window.location.href = window.location.href;
}

function render(array) {
  /*   for (let i=0; i<array.length; i++)*/
  array.forEach(function (el) {
    list.innerHTML += '<input type="checkbox">' + el;
  });
}

function saveState() {
  checkBoxes = document.querySelectorAll('input[type="checkbox"]');
  console.log(checkBoxes);
  checkBoxes.forEach(function (el) {
    currentState = el.checked;
  });
}

function save() {
  nameValue = document.getElementById('name').value;
  //console.log(nameValue);
  let contentValue = document.getElementById('content').value;
  //console.log(contentValue);
  arr = contentValue.split(',');
  arr.forEach(function (el) {
    storage.push(el);
    console.log(storage);
  });
  localStorage.setItem('lists', JSON.stringify(storage));
  render(arr);
  content.value = '';
}

function deleter() {
  localStorage.clear();
  reload();
}

//   console.log (localStorage)}

Proper formatting is pretty much as important as working code is.

I actually rearranged and clear up the code and added the semicolons and it worked . The error just cleared too.
I didn’t give much thought to the semicolons before but I will always add it now
Thank you

I’m glad to hear you were able to clear it up. Happy coding!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.