UX Design and Development course

If else conditions

Fun with if/else and comparison operators

var apple = 20;
var orange = '20';

When comparing use === (strict) is equal to this will also make sure your values are of the same type.

Example: a string equals a string or an integer equals an integer. If you used just '==', apple and orange would be equal. Using just '==' you would get true when comparing 0 == ''

if (apple === orange) {
  console.log('Apples are equal to oranges' + space)
} else {
  console.log('Apples and oranges, not equal' + space)
}

If apple IS NOT EQUAL to orange

if (apple !== orange) {
  console.log('Apples do not equal oranges' + space)
} else {
  console.log('Apples and oranges, living happy together' + space)
}

Compare actual values

if (apple >= orange) {
    console.log('Apples are greater then or equal to oranges' + space)
} else if (apple <= orange) {
    console.log('Apples are less then or equal to oranges' + space)
}

Play with code

JS Bin