Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

localStorage assignment vs. setItem() [duplicate]

One of the first results from a Google search says that this syntax is to be used for localStorage:

localStorage.lastname="Smith";

MDN uses .setItem() and .getItem(), I suppose so that if the browser doesn't support localStorage, then you can add it using the technique defined there.

But since my program is only running on iOS, I guess my question is:

Is it ok to use

localStorage.lastname="Smith";

instead of:

window.localStorage.setItem("lastname","Smith");
like image 975
Phillip Senn Avatar asked Dec 19 '13 18:12

Phillip Senn


1 Answers

Though both methods are valid JavaScript,

localStorage.lastName = "Smith";

assigning a value directly like above to a key can sometimes result in property conflicts.

window.localStorage.setItem("lastname","Smith");

On the other hand is a better practice to get into. MDN and most other resources recommend this method when working with Local-storage.

like image 113
Adi Avatar answered Oct 30 '22 11:10

Adi