 prisskreative
1/30/2015 - 2:07 AM

## Comparisons

Comparisons

``````/* Combining comparisons

You can also combine different comparisons to make a larger, more specific
one using && and ||:

*/

var country, number;

country = "Australia";

// || represents an OR
if (country === "Australia" || country === "New Zealand") {
console.log(country + " is either Australia or New Zealand!");
}

country = "New Zealand";

if (country === "Australia" || country === "New Zealand") {
console.log(country + " is either Australia or New Zealand!");
}

number = 15;

// && represents an AND
if (number > 10 && number < 20) {
console.log(number + " is between 10 and 20.");
}

number = 21;

// No longer runs because number is greater than 20
if (number > 10 && number < 20) {
console.log(number + " is between 10 and 20.");
}

-----------------

//Doing things when if doesn't match

//To catch cases that don't match your if comparison you open an else. Try it:

var number;

number = 21;

if (number > 10 && number < 20) {
console.log(number + " is between 10 and 20.");
} else {
console.log(number + " is NOT between 10 and 20.");
}

//You can also combine else with another if to make more specific logic:

var number;

number = 21;

if (number > 10 && number < 20) {
console.log(number + " is between 10 and 20.");
} else if (number <= 10) {
console.log(number + " is less than 10.");
} else {
console.log(number + " is greater than 20.");
}

//Keep in mind that when using chains of if and else only the section that matches your case will run. The other sections of code will be ignored.``````
``````/* Basic Comparisons

To do that you use if structures in combination with special comparison operators.
If the condition you specify with comparison operators is met,
the instructions inside the if are run
*/

var number = 10;

// Equal
if (number === 10) {
console.log("Number is equal to 10!");
console.log(number);
}

number = number - 1;

// Not equal
if (number !== 10) {
console.log("Number is now NOT equal to 10!");
console.log(number);
}

// Less than
if (number < 10) {
console.log("Number is less than 10!");
console.log(number);
}

number = number + 2;

// Greater than
if (number > 10) {
console.log("Number is now greater than 10!");
console.log(number);
}

// Greater than or equal
if (number >= 10) {
console.log("Number is greater than or equal to 10!");
console.log(number);
}

number = 10;

if (number >= 10) {
console.log("Number is STILL greater than or equal to 10!");
console.log(number);
}

// Less than or equal
if (number <= 10) {
console.log("Number is less than or equal to 10!");
console.log(number);
}

number = number - 2;

if (number <= 10) {
console.log("Number is STILL less than or equal to 10!");
console.log(number);
}

&&   // and (all need to be true)

var age = 25;
( 20 < age && age < 30 ) // true - true = true

var age = 'string';
( 20 < age && age < 30 )  // false - false = false

-------

||   //  or  (just one need to be true)

agree = 'yes';
( agree === 'yes' || agree ==== 'y' ) // true - false = true

agree = 'n';
( agree === 'yes' || agree ==== 'y' ) // false - false = false
``````