![ternary operator javascript ternary operator javascript](https://i.stack.imgur.com/Er8YB.png)
So whether you’re just starting to learn Web Development, or would like to help others learn - Welcome! Please use the buttons below to follow us! More importantly, I look forward to now being able to provide a space for other writers to showcase and share their knowledge. With the creation of Codeburst, I’ll be able to continue to provide tutorials, news, and articles to web development students everywhere. People, more experienced than myself, wrote articles and tutorials and shared them so I could become the developer I am today. I learned everything I know about Web Development from the internet.
![ternary operator javascript ternary operator javascript](https://miro.medium.com/max/2000/1*Kha6v2fI7SbSQsj7woQiGg.jpeg)
Just yesterday I launched codeburst.io - the newest online publication that will showcase web development articles and tutorials from writers all over the world. price is assigned the value of '$6.00' which is then logged to the screen.Our second condition tests isSenior - since this is true, the code after the ? but before the : is run.First we test isStudent - since that is false, the code after the : is executed.That’s a lot of question marks and colons - lets break it down. Here’s what the code for a Senior would look like: let isStudent = false let isSenior = true let price = isStudent ? '$8.00' : isSenior ? '$6.00' : '$10.00' console.log(price) // '$6.00' In this scenario the regular ticket price is $10. The first operand is a condition followed by a question mark (). Javascript’s conditional or ternary operator takes three operands.
TERNARY OPERATOR JAVASCRIPT MOVIE
What if our movie theater gives student discounts and senior discounts? We can nest ternary operators to test multiple conditions. Syntax of conditional (ternary) operator in javascript. If you’re a student, the ticket price is $8, else it’s $10: let isStudent = true let price = isStudent ? '$8.00' : '$10.00' console.log(price) // '$8.00' In this example, we’re programming for a movie theater that has discounted tickets for students. If your condition evaluates to false, anything after the colon is executed. Anything between the ? and the : is what is executed if the condition evaluates to true. A ? separates our conditional from our true value.Our condition is what we’re actually testing.
TERNARY OPERATOR JAVASCRIPT DRIVER
An if statement for this scenario might look something like this: let age = 20 let driver if (age >= 16) For this example, we’ll assume you have to be 16 to drive. Say we have the age of a person and want to assign true if they’re old enough to drive, and false if they aren’t.