From fc1c2f4a124555d1222cbd52fb306d62665045d3 Mon Sep 17 00:00:00 2001 From: CGS-Jack-Bashford <37096442+CGS-Jack-Bashford@users.noreply.github.com> Date: Sat, 3 Nov 2018 01:20:08 +1100 Subject: [PATCH] Added comparison to regular if-else statement (#20756) --- .../conditional-ternary-operators/index.md | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/guide/english/javascript/conditional-ternary-operators/index.md b/guide/english/javascript/conditional-ternary-operators/index.md index 71547b56f9..160fd6c40d 100644 --- a/guide/english/javascript/conditional-ternary-operators/index.md +++ b/guide/english/javascript/conditional-ternary-operators/index.md @@ -5,7 +5,7 @@ title: Conditional Ternary Operators ### Basic usage The ternary operator is a compact way to write an if-else inside an expression. ```js -const thing = (condition) ? : ; +const thing = ? : ; ``` E.g. ```js @@ -18,6 +18,20 @@ You can also chain ternary operators, this way you will have an if-else if-else : ? : ``` +### Comparison to if statement: +The ternary operator is essentially a simplification of the normal `if` statement. You turn this: +```js +if (condition) { + ; +} else { + ; +} +``` +Into this: +```js +var output = ? : ; +``` +As you can see, it is much easier to write and read. > **Pro tip**: As you see you can split the ternary operator on multiple lines E.g. ```