Omitting the second expression when using the if-else shorthand

这一生的挚爱 提交于 2019-11-26 04:04:43

问题


Can I write the if else shorthand without the else?

var x=1;

x==2 ? dosomething() : doNothingButContinueCode();   

I\'ve noticed putting null for the else works (but I have no idea why or if that\'s a good idea).

Edit: Some of you seem bemused why I\'d bother trying this. Rest assured it\'s purely out of curiosity. I like messing around with JavaScript.


回答1:


This is also an option:

x==2 && dosomething();

dosomething() will only be called if x==2 is evaluated to true. This is called Short-circuiting.

It is not commonly used in cases like this and you really shouldn't write code like this. I encourage this simpler approach:

if(x==2) dosomething();

You should write readable code at all times; if you are worried about file size, just create a minified version of it with help of one of the many JS compressors. (e.g Google's Closure Compiler)




回答2:


What you have is a fairly unusual use of the ternary operator. Usually it is used as an expression, not a statement, inside of some other operation, e.g.:

var y = (x == 2 ? "yes" : "no");

So, for readability (because what you are doing is unusual), and because it avoids the "else" that you don't want, I would suggest:

if (x==2) doSomething();



回答3:


Another option:

x === 2 ? doSomething() : void 0;



回答4:


If you're not doing the else, why not do:

if (x==2) doSomething();



回答5:


Using null is fine for one of the branches of a ternary expression. And a ternary expression is fine as a statement in Javascript.

As a matter of style, though, if you have in mind invoking a procedure, it's clearer to write this using if..else:

if (x==2) doSomething;
else doSomethingElse

or, in your case,

if (x==2) doSomething;



回答6:


A tiny addition to this very, very old thread..

Let's say your'e inside a for loop and need to evaluate a variable for a truthy/falsy value with a ternary operator, while in case it's falsy you want to continue - you gonna have a problem because your'e not returning an expression ,you return instead a statement without any value.

This will produce Uncaught SyntaxError: Unexpected token continue

 for (var i = 0; i < myArray.length; i++) {
      myArray[i].value ? alert('yay') : continue;
 }

So, if you do want to return a statement and still use one line for your code, while it may seem kinda weird at first glance and may not follow strict language usage, you can do this instead:

  for (var i = 0; i < myArray.length; i++) {
      if (myArray[i].value) alert('yay') ; else continue;
  }
  • P.S - This code may be hard to read or understand so it will not always be the best option to use. Just saying.. :)



回答7:


Technically, putting null or 0, or just some random value there works (since you are not using the return value). However, why are you using this construct instead of the if construct? It is less obvious what you are trying to do when you write code this way, as you may confuse people with the no-op (null in your case).




回答8:


Probably shortest

x-2||dosomething()

let x=1, y=2;
let dosomething = s=>console.log(s); 

x-2||dosomething('x do something');
y-2||dosomething('y do something');


来源:https://stackoverflow.com/questions/11069278/omitting-the-second-expression-when-using-the-if-else-shorthand

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!