From c29445ca5e5215c15e3eae79284bbfde01215d5a Mon Sep 17 00:00:00 2001 From: JiDarwish <29838474+JiDarwish@users.noreply.github.com> Date: Tue, 20 Nov 2018 18:47:00 +0100 Subject: [PATCH] Added ternary operator (#22234) * Added ternary operator * Update index.md --- .../javascript/if-else-statement/index.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/guide/english/javascript/if-else-statement/index.md b/guide/english/javascript/if-else-statement/index.md index 82b8928a4a..27da46cbc9 100644 --- a/guide/english/javascript/if-else-statement/index.md +++ b/guide/english/javascript/if-else-statement/index.md @@ -43,6 +43,22 @@ if (condition) { MDN link | MSDN link +### Additional ternary operator +For a simple operation like assigning a value to a variable conditionally you can use a ternary operator, which is a shorthand syntax for an `if...else` clause in JavaScript +```javascript +// Normal if...else +let num = 1; +if (someCondition){ + num = 1; +} else { + num = 0; +} + +// Using ternary operator +const num = someCondition ? 1 : 2; +// condition ? true case : false case +``` + ## Examples **Using** `if...else`: