Omitting the second expression when using the if-else shorthand
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) JavaScript shorthand if statement, without the else portion
you can use &&
operator - second operand expression is executed only if first is true
direction == "right" && slideOffset += $(".range-slide").width()
in my opinion if(conditon) expression
is more readable than condition && expression
Python equivalent of omitting second part of ternary operator (a if a else b)
You can use or
here as an empty string evaluates as a Falsey value:
result = myFunc() or False
Refer to Truth Value Testing Python shorthand using only if, excluding the else part
Perhaps you can use:
found_target = (qo_cplId == cpl_id)
It gives you the same boolean output though, True/False. php if else with function shorthand
You don't use the if
keyword when writing a ternary expression.
($abc != '') ? msg($abc) : msg('nope');
ormsg($abc != '' ? $abc : 'nope');
How to use echo along with shorthand IF, ELSEIF and ELSE
Check the variable again if the statement is false and you need to add parenthesis around the entire else block
echo ($gender == 'male') ? 'M' : (($gender == 'female') ? 'F' : 'undefined');
Related Topics
Access 'Data-' Attribute Without Jquery
Unchecked Runtime.Lasterror While Running Tabs.Executescript
What's the API Key for in Google Maps API V3
How to Initialize a JavaScript Date to Midnight
Unexpected Token Illegal in Webkit
Why Do Empty JavaScript Arrays Evaluate to True in Conditional Structures
How to Create a Date Object from String in JavaScript
Authorization of Google Drive Using JavaScript
Create an Array with Random Values
How to Set the Style -Webkit-Transform Dynamically Using JavaScript
JavaScript Roundoff Number to Nearest 0.5
How to Check If an Embedded Svg Document Is Loaded in an HTML Page
In Angular, How to Determine the Active Route
Why Firefox Says That Window.Event Is Undefined? (Call Function with Added Event Listener)
Node.Js Piping the Same Readable Stream into Multiple (Writable) Targets