How to prevent an array storing items from local storage from initializing each time
Hello guys, sorry to disturb you all; I'm working on a to-do list small project; I was able to add items to the localStorage and retrieve them when web page loads but the problem is each time the page loads, the array is emptied, it's re-intialized and I lost my previous saves.
Have a look at my codepen:
https://codepen.io/Fakeur/pen/OJKwvor
Here is my JS code:
11 Replies
You need to check if the localstorage array contains data rather than just declaring an empty array, something like this:
the pipe symbols, what does it do? Like we have 2 conditions, condition 1 and condition 2; if condition 1 fails, then do condition 2 ?
not exactly
the
||
will return the left value if truthy, and will return the right value otherwisehmm something is truthy if it evaluates to True, like if we retrive information in this case for e.g ?
basically, if it isnt
false
, 0
, null
or undefined
im not sure if nan is falsey or notah yep I see, like in this case, if Tasks isn't define and we try to retrieve it, we get null, null is falsy; we move on to the next expression; an empty array is considered as being truthy
undefined
you get undefined
yeah I see
yep I think this concept of truthy and falsy is clearer now, thanks !!!
you're welcome
now, im going to throw a wrench at this: an empty array can be truthy or falsey, depends on how you check
how can it be falsy please
if you do
[] == false
, you get true