![]() ![]() It's the only one JavaScript currently has, though. ![]() They can even be chained: serveDrink(userIsYoungerThan4 ? 'Milk' : userIsYoungerThan21 ? 'Grape Juice' : 'Wine') īe careful, though, or you will end up with convoluted code like this: var k = a ? (b ? (c ? d : e) : (d ? e : f)) : f ? (g ? h : i) : j ġ Often called "the ternary operator," but in fact it's just a ternary operator. But you should be aware of their individual benefits and how they work in JavaScript. Many of them are common to other programming languages too. The JavaScript language provides various ways of using conditional statements. Like all expressions, the conditional operator can also be used as a standalone statement with side-effects, though this is unusual outside of minification: userIsYoungerThan21 ? serveGrapeJuice() : serveWine() A conditional statement let you run a block of code based on a specific condition. ServeDrink(userIsYoungerThan21 ? "Grape Juice" : "Wine") They enable you to control the flow of your programs using. This can be shortened with the ?: like so: var userType = userIsYoungerThan18 ? "Minor" : "Adult" If youre writing programs in JavaScript, conditional statements may be your next best friend. It can only take the values true or false. Boolean Values Very often, in programming, you will need a data type that can only have one of two values, like YES / NO ON / OFF TRUE / FALSE For this, JavaScript has a Boolean data type. Here is an example of code that could be shortened with the conditional operator: var userType The if/else statement is a part of JavaScript's 'Conditional' Statements, which are used to perform different actions based on different conditions. A JavaScript Boolean represents one of two values: true or false. The if.else statement is used to execute a block of code among two alternatives. ![]() This is a one-line shorthand for an if-else statement. ![]()
0 Comments
Leave a Reply. |