js - Use the Conditional (Ternary) Operator

this statement can be used instead of if this else that statements. to me it makes it look a bit more complex when reading it. this is the basic syntax. condition ? statement-if-true : statement-if-false;

Demo


function checkEqual(a, b) {
  return a == b ? true : false;
}

HTML

<div id='result' class='mb-3 alert alert-primary'>..</div>
<button onclick='checkEqual(1,2);' class='btn btn-primary mb-1'>Check Equal 1,2</button>
<button onclick='checkEqual(1,1);' class='btn btn-primary mb-1'>Check Equal 1,1</button>

Result

..

External Link for js - Use the Conditional (Ternary) Operator

Other Items in javascript
Page Views

This page has been viewed 55 times.

No Items Found.

No Items Found.

No Items Found.

No Items Found.

No Items Found.

javascript
...
Use Multiple Conditional (Ternary) Operators

I actually find if then statements much easier to read than these ones, but these seem more efficient. 

3:18 am, September 27, 2019
Search Code
Search Code by entering your search text above.
Welcome

This is my test area for webdev. I keep a collection of code snippits here, mostly for my reference. Also if i find a good site, i usually add it here.

❤👩‍💻🕹