Shorthand for if-else statement

I have some code with a lot of if/else statements similar to this:

var name = "true";
if (name == "true") { var hasName = 'Y';
} else if (name == "false") { var hasName = 'N';
};

But is there a way to make these statements shorter? Something like ? "true" : "false" ...

4

7 Answers

Using the ternary :? operator [spec].

var hasName = (name === 'true') ? 'Y' :'N';

The ternary operator lets us write shorthand if..else statements exactly like you want.

It looks like:

(name === 'true') - our condition

? - the ternary operator itself

'Y' - the result if the condition evaluates to true

'N' - the result if the condition evaluates to false

So in short (question)?(result if true):(result is false) , as you can see - it returns the value of the expression so we can simply assign it to a variable just like in the example above.

9

You can use an object as a map:

 var hasName = ({ "true" : "Y", "false" : "N" })[name];

This also scales nicely for many options

 var hasName = ({ "true" : "Y", "false" : "N", "fileNotFound" : "O" })[name];

(Bonus point for people getting the reference)

Note: you should use actual booleans instead of the string value "true" for your variables indicating truth values.

4

Try this

hasName = name ? 'Y' : 'N';
0

Try like

var hasName = 'N';
if (name == "true") { hasName = 'Y';
}

Or even try with ternary operator like

var hasName = (name == "true") ? "Y" : "N" ;

Even simply you can try like

var hasName = (name) ? "Y" : "N" ;

Since name has either Yes or No but iam not sure with it.

?: Operator

If you want to shorten If/Else you can use ?: operator as:

condition ? action-on-true : action-on-false(else)

For instance:

let gender = isMale ? 'Male' : 'Female';

In this case else part is mandatory.

&& Operator

In another case, if you have only if condition you can use && operator as:

condition && action;

For instance:

!this.settings && (this.settings = new TableSettings());

FYI: You have to try to avoid using if-else or at least decrease using it and try to replace it with Polymorphism or Inheritance. Go for being Anti-If guy.

Most answers here will work fine if you have just two conditions in your if-else. For more which is I guess what you want, you'll be using arrays. Every names corresponding element in names array you'll have an element in the hasNames array with the exact same index. Then it's a matter of these four lines.

names = "true";
var names = ["true","false","1","2"];
var hasNames = ["Y","N","true","false"];
var intIndex = names.indexOf(name);
hasName = hasNames[intIndex ];

This method could also be implemented using Objects and properties as illustrated by Benjamin.

Try this method;

Shorthand method:

let variable1 = '';
let variable2 = variable1 || 'new'
console.log(variable2); // new

Longhand method:

 let variable1 = 'ya'; let varibale2; if (variable1 !== null || variable1 !== undefined || variable1 !== '') variable2 = variable1;
console.log(variable2); // ya

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like