Hi, I have a error. What is the meaning of my array ? I add task but I have error. Also, I see the localstorage variable. I was thinking, if I did not used to local server, i met this problem.
Everything is okey but the only belowing code block has a problem
const tasks = JSON.parse(localStorage.getItem('tasks'));
tasks.forEach(function(task){
console.log(task);
});
Full of code
document.querySelector('form').addEventListener('submit', function(e){
const task = document.getElementById('task').value;
let tasks;
if(localStorage.getItem('tasks') === null){
tasks = [];
} else {
tasks = JSON.parse(localStorage.getItem('tasks'));
}
tasks.push(task);
localStorage.setItem('tasks', JSON.stringify(tasks));
alert('Task saved');
//console.log(task);
e.preventDefault();
});
const tasks = JSON.parse(localStorage.getItem('tasks'));
tasks.forEach(function(task){
console.log(task);
});