Sintaxis if (condicin) sentencia1 else sentencia2 condicin Una expresin que puede ser evaluada como verdadera o falsa. Si la condicin es evaluada como falsa, otra sentencia puede ser ejecutada. It's the only one JavaScript currently has, though. if.else Resumen Ejecuta una sentencia si una condicin especficada es evaluada como verdadera. 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. 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() ServeDrink(userIsYoungerThan21 ? "Grape Juice" : "Wine") This can be shortened with the ?: like so: var userType = userIsYoungerThan18 ? "Minor" : "Adult" About Press Copyright Contact us Creators Advertise Developers Terms Privacy Policy & Safety How YouTube works Test new features NFL Sunday Ticket Press Copyright. Here is an example of code that could be shortened with the conditional operator: var userType This is a one-line shorthand for an if-else statement.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |