Javascript

Home Load Console Selectors CSS Form Strings If Else Array Reg Exp Date & Time setTimeout JSON Loops Objects Animation Fat Arrow Class Import IIFE undefined Tabulator.js ES6 Event Listeners AJAX Hide Elements Create Elements Checkbox ejs
O O

Weather Controls

Time Of Day
Rain
Wind Speed
Wind Direction
Clouds

Javascript : If Else

2022-07-18

If

if (isActive === true) {
    /// ...code
}

Short Circuit

When you have 2 conditions with AND operator, and the first condition is false, js will not even bother to look at the second conditions, it just moves on.

if (isActive === true &&  isNew === true) {
    /// ...code
}

If isActive here is false, js wont even bother checking isNew.

If Else

if (time < 10) {
    greeting = "Good morning";
} else if (time < 20) {
    greeting = "Good day";
} else {
    greeting = "Good evening";
}

Ternary

This is a shortcut code.

var isValid = ("<% =isValid %>".toLowerCase() === "true") ? true : false;

It is equivalent to this:

var isValid = function() {
  if ("<% =isValid %>".toLowerCase() === "true") {
    return true;
  } else {
    return false;
  }
}

switch

switch (robsInt) {
    case 0:
        number = "Its a zero";
        break;
    case 1:
        day = "I'm a number 1";
        break;
    default: 
        text: "Default";
}