8

I'm trying to convert the following if-else to it's ternary operator representation in javascript as follows

var x = 2;
if (x === 2) {alert("2");}
else
         { //do nothing}

But when I do this:

(t==2)?(alert("1")):();

Chrome throws a SyntaxError.

My question is - How to have a ternary operator in javascript with an empty "else" branch - i.e the part that comes after ":". Also, is this allowed- using a ternary operator in javascript to execute statements - NOT do assignment.

Also: the above code was just a base case. I'm actually trying to get all the DOM elements of the page as an array (called all2) and then add only those elements to another array (called only) only if they have non-null class names. Here is my code:

all2.forEach(function(e){ e.getAttribute("class") ? (only.push(e.getAttribute("class"))) : (); }); 

If I leave the third operand blank, it throws a syntax error. Passing a null works

Probosckie
  • 1,585
  • 4
  • 25
  • 40
  • 8
    why are you using a ternary operator if there is no other branch of logic? – Jon Wells Aug 12 '15 at 09:07
  • The ternary operator is called ternary because it requires three operands to operate. What I think would be a more appropriate solution is to use JavaScript short-circuits. Please take a look at my humble posting about JavaScript short-circuits @ http://js-guru.blogspot.com/2015/08/javascript-short-circuiting.html – Allan Chua Aug 12 '15 at 09:09
  • no need of using ternary operator if you dont have else. ternary is short hand method for **if-else**. – Sindhoo Oad Aug 12 '15 at 09:10
  • **Dont** you think you are making things Wayyy complicated than you need to ? – Pratik Joshi Aug 12 '15 at 09:10
  • the above code was just a base case. I'm actually trying to get all the DOM elements of the page as an array (called all2) and then add only those elements to another array (called only) only if they have non-null class names. Here is my code: all2.forEach(function(e){ e.getAttribute("class") ? (only.push(e.getAttribute("class"))) : (); }); If I leave the third operand blank, it throws a syntax error. Passing a null works – Probosckie Aug 12 '15 at 09:16
  • @PratikCJoshi No, this is not complicated at all. – Probosckie Aug 12 '15 at 09:16

7 Answers7

11

Answer to your real question in the comments:

all2.forEach(function (e) {
    e.getAttribute("class") && only.push(e.getAttribute("class"));
});
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
7

Do this :

(t==2)?(alert("1")):null;

You could replace null by any expression that has no side effect. () is not a valid expression.

Pratik Joshi
  • 11,485
  • 7
  • 41
  • 73
potatopeelings
  • 40,709
  • 7
  • 95
  • 119
  • I am actually trying to do something else. The above example was just a base case. I want to parse an array (variable name - all2) and push an element of all2 into another array only if it is non-null – Probosckie Aug 12 '15 at 09:10
5

You putted a lot of useless parentheses, and the best NULL value in js is undefined.

document.getElementById('btn-ok').onclick = function(){
  var val = document.getElementById('txt-val').value;
  
  val == 2 ? alert(val) : undefined;
}
<input id="txt-val" type="number" />
<button type="button" id="btn-ok">Ok</button>

using a single line if statement is better though

if(value === 2) alert(value);
4

you have a few options to do this nicely in one line:

option1 - noop function

set a global noop function:

function noop(){}
(t==2)?(alert("1")):(noop());

option2 - && operator

when you use && operater, operands are evaluted only if previos ones where true, so you could miply write:

(t==2) && alert("1");

or, for exapmle if you have an arry you want to push to, you could test it is not null before:

arr && arr.push(obj)
MoLow
  • 3,056
  • 2
  • 21
  • 41
3

I don't like it's either. So you're on the right track looking for alternatives.

In this case, I would write:

t===2 && alert("2")

Your idea is valid too, for instance you can do this:

t===2 ? alert("2") : null

But it's four extra chars.

4b0
  • 21,981
  • 30
  • 95
  • 142
James M
  • 31
  • 1
2

In that case you don't need to use Ternary operator. Ternary operator requires a third argument.

condition ? expr1 : expr2

Lokki at Conditional (ternary) Operator

You can use the if statement

if ( t == 2 ) alert(1);
Milos Miskone Sretin
  • 1,748
  • 2
  • 25
  • 46
2

NO, you can't have empty else, better don't use the ternary operator, it requires a third argument. Go with simple if condition.

if(t==2) alert("2");
venkat7668
  • 2,657
  • 1
  • 22
  • 26