edencas.blogg.se

Javascript questionmark operator
Javascript questionmark operator











Here, the ternary operator occupies only one line of code, whereas the if.else takes seven lines. Let's compare a ternary operator with a longer if.else statement: The ternary operator is basically a shortcut for a traditional if.else statement. Then we add a colon ( :) followed by a value to return if the condition is false. Starting with ?, we add a condition on the left side and a value on the right side to return when the condition is true. The ? operator is also called the ternary operator because, unlike other operators such as strict equal ( =) or remainder ( %), it's the only one that takes three operands. The term ternary means composed of three items or parts. We'll look at each of these in detail, starting with the most common way you'll see the ? operator being used – as a ternary operator.

javascript questionmark operator

Three Main Uses for the Question Mark ( ?) in JavaScript: There are three main uses for the ? operator, two of which you might not used or even heard of. The ? operator is used in conditional statements, and when paired with a :, can function as a compact alternative to if.else statements.īut there is more to it than meets the eye. The conditional or question mark operator, represented by a ?, is one of the most powerful features in JavaScript.













Javascript questionmark operator