javascript - Multiple statements inside a conditional operator - Stack Overflow

HI,I need to write multiple statements inside a conditional operator.What will be the equivalent of th

HI, I need to write multiple statements inside a conditional operator.

What will be the equivalent of this if condition with conditional operator

var name;
var age;
var passed;

if ( arg == "first" )
{
     name = "John";
     age = "25";
     passed = false;
}
else
{
     name = "Peter";
     age = "29";
     passed = true;
}

HI, I need to write multiple statements inside a conditional operator.

What will be the equivalent of this if condition with conditional operator

var name;
var age;
var passed;

if ( arg == "first" )
{
     name = "John";
     age = "25";
     passed = false;
}
else
{
     name = "Peter";
     age = "29";
     passed = true;
}
Share Improve this question asked May 16, 2009 at 7:29 rahulrahul 187k50 gold badges238 silver badges264 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 3

hmhm, do you mean ternary operator?

var passed = arg == "first"
    ? (name = "John", age = "25", false) 
    : (name = "Peter", age = "29", true);

My quick check with embedjs shows that it does more or less work. But why? Why would you need that? It would qualify as a major WTF.

If you're in a situation where you need to execute statements based on a boolean condition, you should really use if-else. The conditional operator is really meant to return a value from an expression, not to execute full statements. By using the conditional operator, you make your code harder to read and more perilous to debug.

If you insist on using the conditional operator, alamar's solution appears to fit your need quite nicely. However, I remend you vigorously ment your code. The next time you need to modify your code, that ment could be the difference between taking 60 seconds to understand and taking 0.6 seconds to understand.

And if you do ment it, there's really no bandwidth savings in using the character-wise shorter conditional operator over the if-else statement.

Javascript supports object literals - if all you want is to either instantiate one set of variabels or the other, try something like:

var obj = arg == "first" ? 
            { name : "John", age : "25", passed : false } : 
            { name : "Peter",  age : "29", passed : true };

Afterwards, you could refer to name, age, and passed as obj.name, obj.age, and obj.passed. Depending on how monly you use these three variables together, you might wish to choose to make them a real class.

Compared to alamar's solution, this does without side effects (beyond the setting of obj, which will likely make your code easier to maintain in the long run.

If you need to perform multiple operations as part of a conditional check, consider creating a function for the code you listed and call that function in your code where you need the check. This will keep your code neat and your function understandable.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745051933a4608441.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信