The Ternary Operator

I am a full stack developer, YouTuber and blogger!
Search for a command to run...

I am a full stack developer, YouTuber and blogger!
Thanks a lot, Edidiong!
Hey all ๐๐ Webhooks are a powerful tool that allow applications to communicate with each other in real-time. They are often used to send notifications or updates from one system to another without requiring manual intervention. If you know the bas...

Hey everyone! It's been a while since I wrote my last article. But here I am with another topic that might be informational to you ๐. This time I am writing about why you should avoid the onclick attribute in your HTML and JavaScript code. This ar...

โ What and Why Memoriez โ This is a very important question because it makes things clear as to why this app is needed. I built Memoriez because I honestly found it hard to write and maintain journal/diary entries daily. It's not easy to maintain a j...

Explained with a simple project

๐ Hey developers! This post is about how you can sort import statements in your JavaScript/TypeScript/React/Node etc projects easily with Prettier in VS Code when you format the code. https://www.youtube.com/watch?v=QQWgN0_gUxI What will you achieve...

So, if you don't know, my name is Usman and I am a learner. Now let's start!
if else. But shorterThe Syntax is something like this:
CONDITION ? result_if_true : result_if_false
The process is as follows:
name === 'Usman' or something else just like you pass inside of if.true or correct, result_if_true will be returned.false or incorrect, result_if_false will be returned.var company = "Google";
if (company === "Google")
console.log("READY TO WORK!");
else
console.log("I WILL ONLY WORK AT GOOGLE.");
๐ This is the standard if else. You can transform this into a one-liner code.
As I told that result_if_true or result_if_false will be returned, so we will have to store the ternary expression in a variable or in a console.log().
So here, the CONDITION is company === "Google" and if it is true, we should return "READY TO WORK!", else we should return "I WILL ONLY WORK AT GOOGLE.".
So the code will be:
// CONDITION ? result_if_true : result_if_false
console.log(
company === "Google" ? "READY TO WORK!" : "I WILL ONLY WORK AT GOOGLE."
);
var myDecision = company === "Google" ? "READY TO WORK!" : "I WILL ONLY WORK AT GOOGLE.";
console.log(myDecision);
If the company is set to "Google", we will get the output "READY TO WORK!". Else, we will get "I WILL ONLY WORK AT GOOGLE.".
I hope you understood the Ternary Operator clearly. If yes, then it would be nice if you solve this simple CHALLENGE.
I also have a video on my channel about the Ternary Operator
If you have any suggestions or feedback. Please let me know in the comments. Happy Coding!