Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

210
Views
Why won't my stringify parse in an "if" statement?
const friends = ["john","peter","bob"];
localStorage.setItem("friends", JSON.stringify(friends));

const values = JSON.parse(localStorage.getItem("friends"));
console.log(values[2]);
// console.log(values[1]);
// console.log(values[2]);

let fruits;
console.log(fruits);

if (localStorage.getItem('fruits')) {
 fruits = JSON.parse(localStorage.getItem('fruits'));
}
else {
 fruits = [];
}

console.log(fruits);
fruits.push("apple");
fruits.push("orange");
localStorage.setItem("fruits", JSON.stringify(fruits));

Why will the first array behave properly in chrome where I can clearly check the console and application from dev.tools but when I try to make an empty array and push different values fx "apple" and "orange" I can't. My console says the error is in the "if" statement, but I don't understand why. Any thoughts?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I believe the answer lies in the quotes. JSON syntax is very strict and requires the use of double quotes. Change out all your single quotes in your if statement to double quotes and see if that fixes your issue.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!