1

I have the following code which works perfectly, but I'm calling the getItem function twice, which seems unnecessary. How can I tidy it up?

if(localStorage.getItem("preferences") == null {
    //show set preferences page
} else {
    var preferences = localStorage.getItem("preferences");
    //do stuff
}
Brad
  • 1,019
  • 1
  • 9
  • 22
  • Thanks everyone for these suggestions. As they're all pretty much the same I will accept TJs answer as it's the cleanest and makes a good point about reordering the code too. – Brad Dec 01 '16 at 06:08

8 Answers8

2

hope this helps

let preferences = localStorage.getItem("preferences"); 

preferences ? do stuff : show set preferences page
Ajay Gaur
  • 5,140
  • 6
  • 38
  • 60
  • Thanks Ajay, but the 'do stuff' and 'show preferences page' code blocks are too long for this notation in this instance. – Brad Dec 01 '16 at 06:26
  • hey @Brad, you can easily make a function for both, something like: `preferences ? doStuff() : setPreferencesPage()` and write that accordingly. :) – Ajay Gaur Dec 01 '16 at 06:28
1

Just do the getItem once:

var preferences = localStorage.getItem("preferences");
if (preferences == null) {
    //show set preferences page
} else {
    //do stuff
}

And unless it's valid that preferences could be a falsy value other than null, I'd probably invert those blocks:

var preferences = localStorage.getItem("preferences");
if (preferences) {
    //do stuff
} else {
    //show set preferences page
}
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
1
var preferences = localStorage.getItem("preferences");

if(preferences) { 
    //Show preference page
} else { 
    //Do something
}

If the value from local storage is null, you can still assign it to a var and then check against it.

Also, you can simply check the var within the if statement without comparing to null. Take the following for instance:

if(!a) {
    //Will enter here if a is null, undefined, false, empty string, 0 or NaN
}

Further reading: http://james.padolsey.com/javascript/truthy-falsey/

Halaster
  • 1,442
  • 1
  • 17
  • 27
1
var preferences = localStorage.getItem("preferences");
if(!!preferences){ //check if preferences is undefined or false or null
    //show set preferences page
} else {
    //do stuff
}
A.T.
  • 24,694
  • 8
  • 47
  • 65
  • @RahulDesai if variable is null or undefined or false then it will return you false either way, apart from that it also will handle isNaN situation like parseInt("a") to false. for more see http://stackoverflow.com/questions/10467475/double-negation-in-javascript-what-is-the-purpose – A.T. Dec 01 '16 at 06:10
  • `getItem` won't ever return `NaN` though, so it's redundant. – Evan Trimboli Dec 01 '16 at 06:14
  • @EvanTrimboli what if you have "function getItem(){ return parseInt("a") }" – A.T. Dec 01 '16 at 06:16
  • Uh? It's from `localStorage`: https://developer.mozilla.org/en-US/docs/Web/API/Storage/getItem – Evan Trimboli Dec 01 '16 at 09:50
  • ah, ok that was not mentioned in OP question – A.T. Dec 01 '16 at 09:57
0

you need not read localStorage.getItem twice.you can read it into a variable and use the same

var preferences = localStorage.getItem("preferences");
if (preferences == null) {
    //show set preferences page
  } else {
    //you can use this preferences here
    //do stuff
  }

Hope it helps

Geeky
  • 7,420
  • 2
  • 24
  • 50
0

Call it once and store it in a variable.

var preferences = localStorage.getItem("preferences");
if(preferences==null){
    //....
}
else{
    //....
}
Govind Balaji
  • 639
  • 6
  • 17
0

Simple!

var preferences = localStorage.getItem("preferences");

if(preferences == null) {
    //show set preferences page
} else {

    //do stuff with preferences
}
teroi
  • 1,087
  • 10
  • 19
0

Just as an alternative, you could assign preferences within the if statement itself and do the following:

if ((preferences = localStorage.getItem("preferences"))) {
    // use preferences here
}
else {
    // set preferences here
}

I generally take this approach when its something only I'll be working on, assigning within a condition can confuse some people when debugging.

Tim Sheehan
  • 3,994
  • 1
  • 15
  • 18