Use Multiple Conditional (Ternary) Operators

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

CSS

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

JS


// 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);
console.log(checksign_result);

// 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;
}


HTML

<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 346 times.

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.

❤👩‍💻🕹

Random Quote
You want to be the best, you MUST put the long yards in! Nothing comes easy in life so stop wishing and start DOING! So many people would rather bitch and moan than help themselves. Dont be one of those negative drainers, start today, make a small change and keep going forwards with this attitude!
Unknown