3.6 Ternary Operators
Hello to all and welcome to this demonstration or we will see so the ternary or conditional operators in JavaScript. Let's imagine that we want to implement a rule like this: if a customer has more than 100 points, he is a 'gold' customer otherwise he is a 'silver' customer. How do we implement this? Well, first of all we will declare a variable to keep track of the number of points. We make a let points and assign it a value of 110. We want to declare another variable called type that represents the type of customers and here is where we use the ternary or conditional operator. This is how it works. First we start with a condition. We want to compare the number of points to 100, so here we use the comparison operator: points > 100. Now, As you learned earlier, this expression produces a boolean, the result of this expression returns either true or false. Depending on the result, we will set this variable type to a different value. Here we add a '?', so if this expression value is true it means that it is a gold client set the type to gold. Otherwise, if this expression returns false, we set it to 'silver'. So this is the ternary or conditional operator, to start with a condition so if this condition has the value true, we use this value. Otherwise we use this other value. Now we save the changes and we have a gold client, but if I change the number of points to 90 and save the changes we get a silver client. That's it for this little demonstration on ternary or conditional operators. Let's meet again for the next video where we will talk about logical operators.