forked from loverajoel/jstips
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d51b30a
commit 89a8d07
Showing
1 changed file
with
33 additions
and
0 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
_posts/en/javascript/2021-02-02-what-is-the-javascript-ternary-operator.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
--- | ||
layout: post | ||
|
||
title: What is the JavaScript ternary operator? | ||
tip-number: 81 | ||
tip-username: loverajoel | ||
tip-username-profile: https://www.twitter.com/loverajoel | ||
tip-tldr: The method received as an argument for the promise. | ||
|
||
categories: | ||
- en | ||
- javascript | ||
--- | ||
The ternary operator is a shortcut for the `if` statement. It consists of three operands; a question mark, a condition, and an expression to execute if the condition is true, followed by a colon and another expression to execute if it’s false. | ||
|
||
```js | ||
let age = 26; | ||
|
||
// condition ? expression if true : expression if false | ||
let drink = (age >= 21) ? "Beer" : "Juice"; | ||
|
||
console.log(drink); // "Beer" | ||
|
||
// Equivalent to: | ||
let drink; | ||
if ((age >= 21)) { | ||
drink = "Beer"; | ||
} else { | ||
drink = "Juice"; | ||
} | ||
|
||
console.log(drink); // "Beer" | ||
``` |