Use Multiple Conditional (Ternary) Operators

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


* {
  box-sizing: border-box;
  transition: all 0.2s;
  scroll-behavior: smooth;


// this will check if a number is positive negative or zero and return that text
function checkSign(num) {
  return (num === 0) ? "zero" : (num > 0) ? "positive" : "negative";

var checksign_result = checkSign(10);

// this is a common function just to write the content into the result div
function write_result(result) {
  var result_html = document.getElementById("result");
  result_html.innerHTML = result;


<div id='result' class='mb-3 alert alert-primary'>..</div>
<button onclick="write_result(checkSign(10));" class='btn btn-primary mb-1'>Run checkSign 10</button>
<button onclick="write_result(checkSign(-5));" class='btn btn-primary mb-1'>Run checkSign -5</button>
<button onclick="write_result(checkSign(0));" class='btn btn-primary mb-1'>Run checkSign 0</button>

Working Result


External Link for Use Multiple Conditional (Ternary) Operators

Other Items in javascript
Page Views

This page has been viewed 248 times.

No Items Found.

No Items Found.

No Items Found.

No Items Found.

No Items Found.

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-fa..

6:23 am, September 24, 2019
Search Code
Search Code by entering your search text above.

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.


Random Quote
There is no way to happiness. Happiness is the way.