Get full access to
Learn ECMAScript - Second Edition
and 60K+ other titles, with a free 10-day trial of O'Reilly.
There are also live events, courses curated by job role,
and more.
Start your free trial
A
ll three,
localStorage.getItem('key')
,
localStorage.key
, and
localStorage['key']
methods, do the same thing. However, it is advisable to use the provided methods for the following reasons:
localStorage.getItem('key-does-not-exist')
returns
null
, whereas
localStorage['key-does-not-exist']
will return
undefined
. In JavaScript,
null
is not equal to
undefined
.
For example, s
uppose that you want to set a key that is actually the property of an object as well, or as a function name, such as
getItem
and
setItem
. In this case, you're better off with the
getItem
approach, as follows:
localStorage.setItem('getItem', 'whohoo we are not overwriting getItem'); // #1localStorage.getItem('getItem'); ...
Get
Learn ECMAScript - Second Edition
now with the O’Reilly
learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and
nearly 200 top publishers.
Start your free trial
Don’t leave
empty-handed
Get Mark Richards’s
Software Architecture Patterns
ebook to better understand how to design components—and how they should interact.
It’s yours, free.
Get it now
Check it out now
on O’Reilly
Dive in for free with a 10-day trial of the O’Reilly learning platform—then explore all the other resources our members count on to build skills and solve problems
every day.
Start your free trial
Become a member now