简体   繁体   中英

No instruction in javascript

Let us say i have a code like this ternary: element ? doSomething() : doNothing; element ? doSomething() : doNothing; , I wish to know if there is a keyword to say do nothing using this ternary instruction. That is what should I put to say do nothing without using an empty function. I tried putting just the semi-colon it gives me an error.

您可以使用逻辑AND &&代替。

element && doSomething();

只要使用if你有没有使用的打算else速记语法-part反正。

if (element) doSomething ();

Using the ternary you could do:

element ? doSomething() : null;

This is probably the best approach as you wouldn't want to return a boolean or some other value in the place of null.

It will return null if the expression is not true, which allows you to check for the null value elsewhere if you have assigned it to a variable.

Use Logical AND( && ) instead of ternary operator!

element && doSomething();

expr1 && expr2 , Returns expr1 if it can be converted to false ; otherwise, returns expr2 . Thus, when used with Boolean values, && returns true if both operands are true ; otherwise, returns false .

如果您真的想使用三元表达式:

element ? doSomething() : true

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM