✅ – Liil' Boo – 08-41 Aug 10

Hey Would something bad happen (or nothing), if I try to do
JSON.parse(thing)
JSON.parse(thing)
and 'thing' is not an object or array ? Or does that change nothing ?
LB
Liil' Boo616d ago
!title <JSON.parse question>
UU
Unknown User616d ago
LB
Liil' Boo616d ago
JSON.parse() - JavaScript | MDN
The JSON.parse() method parses a JSON string, constructing the JavaScript value or object described by the string. An optional reviver function can be provided to perform a transformation on the resulting object before it is returned.
UU
Unknown User616d ago
LB
Liil' Boo616d ago
Could be anything else What i'm really trying to is
JSON.parse(window.LocalStorage.getItem('key')
JSON.parse(window.LocalStorage.getItem('key')
And I want to be sure how to catch error And if it always returns a string Documentation didn't help me figure it out Or I didn't understand
UU
Unknown User616d ago
LB
Liil' Boo616d ago
Wait, what If I stringify everything before set to Local Storage ? Then I would always deal with strings, right ? As such
window.localStorage.setItem('key', JSON.stringy(value)
window.localStorage.setItem('key', JSON.stringy(value)
UU
Unknown User616d ago
LB
Liil' Boo616d ago
It's purely theoritical, I'm not coding (Sorry I should have mentioned it) Well, thank you for your time I go with the approach of stringify-ing everything
M
marz616d ago
either way localstorage can only store strings
UU
Unknown User616d ago
LB
Liil' Boo616d ago
I guess so, But documentation doesn't say what happens if I try
localStorage.setItem('key', true)
localStorage.setItem('key', true)
Would it become the string 'true' ? Or throw an Error
UU
Unknown User616d ago
LB
Liil' Boo616d ago
As a string ? So localStorage can store booleans ? 😮
E
Eva616d ago
It'll stringify it yes
UU
Unknown User616d ago
LB
Liil' Boo616d ago
Thanks for confirmation
E
Eva616d ago
As if you set .setItem('... ', {}); it'll just stringify it into [object Object]
LB
Liil' Boo616d ago
window.localStorage.setItem('key', JSON.stringy(value) 
window.localStorage.setItem('key', JSON.stringy(value) 
I'll stick to this approach, stringying everything, I'll think about accuracy* later Thanks timo, thanks Eva
UU
Unknown User616d ago
LB
Liil' Boo616d ago
I thought JSON.stringify would be harmless on things that are already a string
UU
Unknown User616d ago
LB
Liil' Boo616d ago
That's bad?
UU
Unknown User616d ago
LB
Liil' Boo616d ago
Well, better safe than sorry I'll go with your advice, dear stranger Thanks again
UU
Unknown User616d ago
LB
Liil' Boo615d ago
!close
UU
Unknown User615d ago