Ternary operator - History

What’s the idea behind “ternary operator” in programming?
In my opinion this is more confusing and it’s not added legibility to our code. Who create the idea of “ternary operator” and which cases we need to use this approach?

Thanks

Ternary operators definitely get abused, especially by new programmers. They very often make code harder to read, and your team will ask you to change them in code review.

So why do they exist? Because there are some simple cases where they actually are easier to read.

Here’s a simple example:

let name = "";
if (user.loggedIn) {
    name = user.name;
}
else {
    name = "new friend";
}
console.log("Hello " + name + "!");

vs.

const name = user.loggedIn ? user.name : "new friend";
console.log("Hello " + name + "!")
2 Likes

Thanks @ArielLeslie!