JSON.stringify()
Convert object or array data into a string.
// Save an object
let lunch = {
sandwich: 'turkey',
chips: 'Cape Cod',
drink: 'Pepsi'
};
localStorage.setItem('lunchOrder', JSON.stringify(lunch));
// Save an array
let drinks = ['Pepsi', 'water', 'lemonade'];
localStorage.setItem('drinkOptions', JSON.stringify(drinks));
⏰ Preorder my new course on Web Components! Want to learn how to build Web Components from scratch, master best practices, and more? Preorder today and get $100 off of the launch price.
Find this useful? You can support my work by purchasing an annual membership.