Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Square bracket syntax vs functions for localStorage [duplicate]

What is the difference between the following two snippets of code? Is the square bracket syntax an old, deprecated syntax? When I first used localStorage, all the documentation I found definitely said to use the square bracket syntax, but now I can't find any documentation on it at all.

The documented syntax:

localStorage.setItem('hello', 'world');
localStorage.getItem('hello'); // world

The square bracket syntax:

localStorage.hello = 'world';
localStorage.hello; // world
like image 577
callumacrae Avatar asked Nov 14 '12 17:11

callumacrae


1 Answers

This is what the documentation says about it:

Although the values can be set and read using the standard JavaScript property access method, using the getItem and setItem methods is recommended.

like image 150
bfavaretto Avatar answered Sep 17 '22 13:09

bfavaretto