Omitting the second expression when using the if-else shorthand

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. … Read more

Ternary operator in PowerShell

From what I know, PowerShell doesn’t seem to have a built-in expression for the so-called ternary operator. For example, in the C language, which supports the ternary operator, I could write something like: <condition> ? <condition-is-true> : <condition-is-false>; If that doesn’t really exist in PowerShell, what would be the best way (i.e. easy to read … Read more

PHP ternary operator vs null coalescing operator

Can someone explain the differences between ternary operator shorthand (?:) and null coalescing operator (??) in PHP? When do they behave differently and when in the same way (if that even happens)? $a ?: $b VS. $a ?? $b 14 s 14 When your first argument is null, they’re basically the same except that the … Read more

How do you use the ? : (conditional) operator in JavaScript?

In simple words, what is the ?: (conditional, “ternary”) operator and how can I use it? 19 s 19 This is a one-line shorthand for an if-else statement. It’s called the conditional operator.1 Here is an example of code that could be shortened with the conditional operator: var userType; if (userIsYoungerThan18) { userType = “Minor”; … Read more