Are you learning JavaScript and came across the term ternary operator? In this article, we will learn what is ternary operator in JavaScript or TypeScript and also how it is implemented.
What is Ternary Operator?
Well, Ternary Operators are insanely helpful when you’re trying to write concise and quick lines of code. Basically, Ternary Operator works exactly like “if-else” conditioning, but instead of using those traditional “if” and “else” clauses with their coding block, Ternary Operator can be a 1 liner. It checks if the condition is true or false and acts accordingly. Simple right!
Let’s look at an example of how exactly ternary operator in JavaScript works:
Instead, you do this whole thing in just 1 line with ternary operator, like so:
var a = 25;
var b = 22;
If( a > b ){
console.log( "a is greater than b" );
}
else {
console.log( "b is greater than a" );
}
a>b ? console.log( "a is greater than b" ) : console.log( "b is greater than a" )
The condition before the “?” symbol, if its true, then the code right after the “?” will get executed and it won’t ahead from there. But if that condition is false, it will directly run the code that’s after the “:” symbol.
And you’re done!
Recent Posts
Recent Comments
Archives
- October 2025
- August 2025
- June 2025
- October 2024
- September 2024
- August 2024
- June 2023
- May 2023
- March 2023
- December 2022
- November 2022
- October 2022
- September 2022
- August 2022
- July 2022
- June 2022
- May 2022
- April 2022
- February 2022
- January 2022
- December 2021
- November 2021
- October 2021
- September 2021
- August 2021
- July 2021
Categories
- Angular
- AWS
- Backend Development
- Big Data
- Casino
- Cloud
- Database
- Deployment
- DevOps
- Docker
- Frontend Development
- GitHub
- Google Cloud Platform
- Installations
- Java
- JavaScript
- Linux
- MySQL
- Networking
- NodeJS
- Operating System
- Python
- Python Flask
- Report
- Security
- Server
- SpringBoot
- Subdomain
- TypeScript
- Uncategorized
- VSCode
- Webhosting
- WordPress
Search
Recent Post
Lopebet’s Multi-Bet Options: How and When to
- 23 October, 2025
- 2 min read
Understanding Bonuses and Promotions Offered by Tez888
- 30 August, 2025
- 2 min read
Hvordan norske mobilcasino støtter veldedige formål og
- 5 June, 2025
- 2 min read