How can I check if an item is set in localStorage
? Currently I am using
if (!(localStorage.getItem("infiniteScrollEnabled") == true || localStorage.getItem("infiniteScrollEnabled") == false)) {
// init variable/set default variable for item
localStorage.setItem("infiniteScrollEnabled", true);
}
This question is related to
javascript
html
local-storage
How can one test existence of an item in localStorage
?
this method works in internet explorer.
<script>
try{
localStorage.getItem("username");
}catch(e){
alert("we are in catch "+e.print);
}
</script>
The shortest way is to use default value, if key is not in storage:
var sValue = localStorage['my.token'] || ''; /* for strings */
var iValue = localStorage['my.token'] || 0; /* for integers */
easist way is
if(localStorage.test){
console.log("now defined");
}
else{
console.log("undefined");
localStorage.test="defined;"
}
How it works
when you call localStorage.test first time it does not contain any store into localStorage object so it returns undefined else condition triggers. after else triggered i set new variable and again check it contains data so it return data with true in if condition
For TRUE
localStorage.infiniteScrollEnabled = 1;
FOR FALSE
localStorage.removeItem("infiniteScrollEnabled")
CHECK EXISTANCE
if (localStorage[""infiniteScrollEnabled""]) {
//CODE IF ENABLED
}
if(!localStorage.hash) localStorage.hash = "thinkdj";
Or
var secret = localStorage.hash || 42;
You can also try this if you want to check for undefined:
if (localStorage.user === undefined) {
localStorage.user = "username";
}
getItem is a method which returns null if value is not found.
localStorage['root2']=null;
localStorage.getItem("root2") === null //false
Maybe better to do a scan of the plan ?
localStorage['root1']=187;
187
'root1' in localStorage
true
You should check for the type of the item in the localStorage
if(localStorage.token !== null) {
// this will only work if the token is set in the localStorage
}
if(typeof localStorage.token !== 'undefined') {
// do something with token
}
if(typeof localStorage.token === 'undefined') {
// token doesn't exist in the localStorage, maybe set it?
}
Try this code
if (localStorage.getItem("infiniteScrollEnabled") === null) {
} else {
}
You can use hasOwnProperty
method to check this
> localStorage.setItem('foo', 123)
undefined
> localStorage.hasOwnProperty('foo')
true
> localStorage.hasOwnProperty('bar')
false
Works in current versions of Chrome(Mac), Firefox(Mac) and Safari.
I've used in my project and works perfectly for me
var returnObjName= JSON.parse(localStorage.getItem('ObjName'));
if(returnObjName && Object.keys(returnObjName).length > 0){
//Exist data in local storage
}else{
//Non Exist data block
}
Best and Safest way i can suggest is this,
if(Object.prototype.hasOwnProperty.call(localStorage, 'infiniteScrollEnabled')){
// init variable/set default variable for item
localStorage.setItem("infiniteScrollEnabled", true);
}
This passes through ESLint's no-prototype-builtins
rule.
Source: Stackoverflow.com