Brad
Brad

Reputation: 1047

Cleaner way to write this code

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
}

Upvotes: 1

Views: 71

Answers (8)

Halaster
Halaster

Reputation: 1502

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/

Upvotes: 1

Tim Sheehan
Tim Sheehan

Reputation: 4014

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.

Upvotes: 0

A.T.
A.T.

Reputation: 26312

var preferences = localStorage.getItem("preferences");
if(!!preferences){ //check if preferences is undefined or false or null
    //show set preferences page
} else {
    //do stuff
}

Upvotes: 1

teroi
teroi

Reputation: 1087

Simple!

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

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

    //do stuff with preferences
}

Upvotes: 0

Govind Balaji
Govind Balaji

Reputation: 639

Call it once and store it in a variable.

var preferences = localStorage.getItem("preferences");
if(preferences==null){
    //....
}
else{
    //....
}

Upvotes: 0

Ajay Gaur
Ajay Gaur

Reputation: 5270

hope this helps

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

preferences ? do stuff : show set preferences page

Upvotes: 2

Geeky
Geeky

Reputation: 7498

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

Upvotes: 0

T.J. Crowder
T.J. Crowder

Reputation: 1074178

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
}

Upvotes: 1

Related Questions