Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Null object is String type in localStorage

I am coming from Java dev and new to javascript, can someone explains what is going on.

I am using localStorage to store my token in my browser.

localStorage.token = 'xxx'

When the users sign out, we remove the token, here is the code

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

localStorage.token = null

If we want to check wether the user is authenticated or not, we check the token

const isAuth = localStorage.token !== null

Here is the thing becomes weird to me. After I marked localStorage.token to null,

the result of localStorage.token !== null is still true !

I had done some investigation,

I logged the localStorage object and found out the token variable is "null" instead of null.

Here is my assumption.

When the browser needs to store the localStorage, it iterates inside the object fields, after finding the null object, it use toString to store the final form?

>Solution :

give a value or Sovrascrivi the one present in the localstorage with the function set item, like code below :

localStorage.setItem('token' , 'your token here ');

and for access the value :

localStorage.getItem('token')
window.console.log("token :",localStorage.getItem('token'))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading