Can a ternary operator have one of its resulting expressions do nothing? If so, how?

Often when I am making simple if statements, I could make things a lot more concise and readable by using a ternary operator, but don’t know how to, because I want the conditional to do nothing in one of the cases. It throws a syntax error if I just leave an empty space either to the left or right of the colon. Is there a way to make it do nothing and only do whatever I code to the left or right of the colon?

For example:

let skyColor = "blue";
skyColor === "black" ? console.log("It is dark outside.") : (do nothing) ;

I have tried using things like continue(despite knowing it is intended for loops) or pass, but none seem to work. Couldn’t find anything about that possibility in the MDN, either.

Hey,
A tenary operator doesn’t have the option for “do nothing” for either the true or false path since a tenary operator returns a value. It must have both values. The reason of having something in place of (do Nothing) is because you’re telling the code, "if this doesn’t satisfy, do this. The purpose of the tenary operator is to run one of the two expressions.
Consider it like this:

test = true;
if (test) {
   ourVariable = doThis;
{
else{
   ourVariable = doThat;
}

In this case, if you want only one value to run/be assigned to, it simplifies to the following.

ourVariable = doThis;
2 Likes

You could do something like this:

console.log("Meaningless text.");
let skyColor = 'blue';
console.log(skyColor === 'black' ? 'It is dark outside.' : ''); //prints a blank line if the condition is false
console.log("Text to show a blank line was printed above.");

Output:

Meaningless text.

Text to show a blank line was printed above.

However, a ternary expression is really a shortcut for an if...else where we only need a single expression for each condition. When we don’t have an else, a simple if statement is much more efficient.

console.log("Meaningless text.");
let skyColor = 'blue';
if(skyColor === 'black') console.log('It is dark outside.');
console.log("More meaningless text.");

Output:

Meaningless text.
More meaningless text.

This would be a good use of a ternary expression:

let skyColor = 'blue';
console.log(skyColor === 'black' ? `It is dark outside.` : `The sky is ${skyColor}.`);

The sky is blue.

3 Likes

You could use null: (skyColor === "black") ? console.log("It is dark outside.") : null;
But, as others have pointed out, the value will be returned so be careful.

2 Likes

You could put any value at all after the :, since the value will be returned to nowhere, but that just further illustrates why it’s silly to use a ternary expression when there is no else.

console.log("Meaningless text.");
let skyColor = 'blue';
skyColor === 'black' ? console.log('It is dark outside.') : null;
skyColor === 'black' ? console.log('It is dark outside.') : undefined;
skyColor === 'black' ? console.log('It is dark outside.') : true;
skyColor === 'black' ? console.log('It is dark outside.') : false;
skyColor === 'black' ? console.log('It is dark outside.') : 11;
skyColor === 'black' ? console.log('It is dark outside.') : 'cat';
console.log("Meaningless text.");

Output:

Meaningless text.
Meaningless text.

2 Likes

Wait, wouldn’t this just reassign the value of the variable?

Yeah it would. That example was only to show that you don’t necessarily need an else if if you’ve got no value to put in it. The other replies are much more detailed than mine. I’d trust those replies more than mine.

1 Like

Thank you all for the replies, very interesting stuff. Whole idea was to also make very short one liner if statements with no else, without having to use curly brackets and such.

I wasn’t aware the if statement could be shortened like that, so that fits the bill nicely!

1 Like