As a rule I use ternary only if the If/Else is a ‘single line of code’ <— Yes this is variable depending on screen size. If Else statement along with commands in if and else blocks could be written in a single line. One way is to use a ternary operator which works as a single line if statement in JavaScript. If you have an if else expression there is also a shorthand for that. javascript conditional-operator. In this tutorial I will share different examples to help you understand and learn about usage of ternary operator in one liner if and else condition with Python. When using if , else if , else statements there are few points to keep in mind. Nested syntax example 3. Multiline syntax example 2. : is called a ternary operator and acts just like an if / else when used in an expression Examples of the single line if statement JavaScript Ternary operator (Conditional operator) Sample Code