The Ternary Operator
Explore the ternary operator as a concise alternative to the if/else statement in JavaScript and TypeScript. Understand how it returns values consistently, supporting functional programming principles, and learn the importance of readable, value-returning expressions.
We'll cover the following...
The if/else statement
Let’s see how this statement works:
We all have choices to make. This is why all programming languages offer some variation of the if/else statement.
The code snippet above prints beer when thirsty is true and hamburger when thirsty is false.
Note: Some languages are very strict about their booleans. For example, Java and C# would require a boolean for
thirstyand reject anything else. Other languages, like Python and JavaScript, are true to their dynamic nature. They are more flexible and only require the values to be. This means they will ... truthy In JavaScript, a value is truthy if JavaScript’s built-in type coercion converts it to true.