Skip to content

Instantly share code, notes, and snippets.

@ryan-w-moore
Created November 11, 2014 09:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryan-w-moore/f302caed4a4c421d27e8 to your computer and use it in GitHub Desktop.
Save ryan-w-moore/f302caed4a4c421d27e8 to your computer and use it in GitHub Desktop.
Operators
// I. Comparison Operators:
if ( a == b ) {... // == Equality
if ( a != b ) {... // != Not Equal to
if ( a === b ) {... // === Strictly Equal to
if ( a !== b ) {... // !== Not Strictly Equal to
if ( a > b ) {... // > Greater Than
if ( a < b ) {... // < Less Than
if ( a >= b ) {... // >= Greater Than, or Equal to
if ( a <= b ) {... // <= Less Than, or Equal to
// II. What if we have a more complex comparison?
// 1. LOGICAL AND:
// What if I want to know if a is stricly equal to b AND c is stricly equal to d?
if (a === b AND c === d) {...
// Then we use the double ampersand &&
// The Double Ampersand is known as the 'LOGICAL AND'
// 'The Logical AND' &&
if (a === b && c === d) {...
// 2. Logical OR:
// What if I want to know if a is strictly equal to b OR c is stricly equal to d?
if (a === b OR c === d) {...
// Then we use the double "Pipes" ||
// The Double Pipes is known as the 'LOGICAL OR'
// 'The Logical OR' ||
if (a === b || c === d) {...
// You can even separate these conditionals on multiple lines taking advantage of js ignoring whitespace ( like so... ):
if (
(a > b)
&&
(c < d ) ) {...
// III. MODULUS: or, Remainder Operator
// The MODULUS Operator gives us the remainder of the two operands...
// Using MODULUS to calculate a leap year:
var year = 2003;
var remainder = year % 4; // <-- = 3. B/c 4 Goes into year 500 times and the remainder would be 3.
// IV. Increment / Decrement: (Unary Operator - Only needs one operand to operate on...)
a = a + 1;
a += 1;
a++;
// or..
a = a - 1;
a -= 1;
a--;
// However there is a tricky difference... PREFIX and POSTFIX
// V. PREFIX / POSTFIX:
var a = 5;
alert(++a); // <-- alert will show a = 6, var a changes BEFORE the alert!
var a = 5;
alert(a++); // <-- alert will show a = 5, var a changes AFTER the alert!
// The end result is the same but the message alert(); was different!
// VI. TERNARY OPERATOR
// Unary Operators: (Takes a single operand)
++a
// Binary Operators: (Takes two operands)
a + b
// Ternary Operators: (Takes three operands!)
condition ? true : false
// you can think of it as a mini if-else statement.
// FOR EXAMPLE:
// if we we to create two player variables and set their values to a given numeric score each, and, without using a
// ...ternary operator or condition would look something like thus:
var playerOne = 500;
var playerTwo = 600;
// sometime later...
var highScore;
if (playerOne > playerTwo)
{
highScore = playerOne;
}
else
{
highScore = playerTwo;
}
}
// HOWEVER, using a Ternary operator we can consolidate the
// ...above six lines of code into about 2 or 3 lines, thusly:
// Remember the ternary operator takes a condition and then creates a
// ...conditional logic-switch-gate that is dependent on the conditional
// ...being either true or false and then acting on the resulting logical outcome.
// Thus, using a Ternary Operator instead for our above example:
var playerOne = 500;
var playerTwo = 600;
//aleternatively...
//var highScore = // The Ternary Operator
var highScore = (playerOne > playerTwo) ? playerOne : playerTwo;
// another way of saying line 128 is:
// set the variable highScore equal to either playerOne or playerTwo...
// depending on whether playerOne's score is greater than playerTwo's score.
// if playerOne IS greater than playerTwo, than highScore = playerOne.
// if playerOne IS NOT GREATER THAN playerTwo, set highScore = playerTwo.
// in the above case:
highScore; // <-- 600 | Since playerOne !> playerTwo
// (500 !> 600) so highScore = playerTwo.
// Equality Operators
// = <-- Assignment
// == <-- Equality
// === <-- Strict Equality
// ==
var a = 5;
var b = '5';
if ( a === b ) {
alert("Yes, they're equal!")
}else{
alert("No, they're NOT equal")
};
// ===
var a = 5;
var b = '5';
if ( a === b ) {
alert("Yes, they're equal!")
}else{
alert("No, they're NOT equal")
};
// Arithmatic Operators
score = score + 10;
// Short hand...
score += 10;
// Operator Precedence (Multiplication and Division is MORE IMPORTANT than Addition and Subtraction)
result = 5 + 5 * 10;
// if unsure wrap in parens
result = 5 + (5 * 10);
// both will give the same result. . .
// Assignment instead of Equality:
var a = 5;
var b = 10;
if (a = b) {
// always true!
}
// Why? Keep in mind, one equals sign is an ASSIGNMENT operator it is a command
// Thus a = b is the command set the value of a equal to b thus b now equals 5 too!
if ( a == b ) {
alert("Yes, they're equal");
// Here we use the double equals which is the equality operator, we are ASKING if they are equal.
}else{
alert("No, they're not equal"):
}
// EXAMPLE ( '==' THE DOUBLE EQUALS - THE EQUAL OPERATOR )
var a = 5;
var b = '5';
if ( a == b ) {
alert("Yes, they're equal");
}else{
alert("No, they're not equal"):
}
// <- "Yes, they're equal"
// Three equals sign is STRICT EQUALITY. Asking if the operands are NOT JUST EQUAL BUT THE SAME /or IDENTICAL
// EXAMPLE of '===' IDENTICAL OPERATOR
var a = 5;
var b = '5';
if ( a === b ) {
alert("Yes, they're identically equal")
}else{
alert("No, they're not identically equal")
}
//<-- No, they're not indentically equal"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment