Skip to content

Instantly share code, notes, and snippets.

@joannasese
Last active April 30, 2017 17:59
Show Gist options
  • Save joannasese/aa9cab43a67b4b9a63d19d9be9611cb8 to your computer and use it in GitHub Desktop.
Save joannasese/aa9cab43a67b4b9a63d19d9be9611cb8 to your computer and use it in GitHub Desktop.
Operators
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="Operators">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Operators</title>
</head>
<body>
<script id="jsbin-javascript">
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="Operators">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Operators</title>
</head>
<body>
</body>
</html>
</script>
</body>
</html>
/*
OPERATORS: Operators act on values. This is easier to show:
1. Assignment
The assignment operator = sets a variable to equal a value. We've seen this operator before.
*/
var housePlant = "fiddle leaf fig"
/*
2. Arithmetic
You guessed it! Arithmetic operators apply mathematical operations to values.
2a. Addition: Adds values. Works not only with numbers, but also with strings. When strings are added, we call this concatenation.
*/
var housePlants = "fiddle leaf fig" + ", " + "hoya";
console.log(housePlants); // prints -> "fiddle leaf fig, hoya"
var numberPlants = 1 + 1;
console.log(numberPlants); // prints -> 2
// Addition can also be expressed by +=.
var housePlants = "fiddle leaf fig";
housePlants += ", hoya";
console.log (housePlants); // prints "fiddle leaf fig, hoya"
var x = 10;
x += 5; // same as x = x + 5
console.log(x); // prints -> 15
// There's also an operator that adds one - "increment." It only really works with numbers.
x++ // This is the same as x = x + 1
// 2b. Subtraction: Subtracts numbers.
var difference = 2 - 1;
console.log(difference); // prints -> 1
var x = 10;
x -= 5; // same as x = x - 5
console.log(x); // prints -> 5
//Like increment, there is decrement.
x-- // This is the same as x = x - 1
// 2c. Multiplication: Multiplies numbers.
var product = 4 * 5;
console.log(product); // prints -> 20
var x = 10;
x *= 5; // same as x = x * 5
console.log(x); // prints -> 50
// 2d. Division: Divides numbers.
var quotient = 66 / 6;
console.log(quotient); // prints -> 11
var x = 10;
x /= 5; // same as x = x / 5
console.log(x); // prints -> 2
// 2e. Modulus. This one can be a little confusing, but basically it's the remainder after division. Check it out.
var remainder = 5 % 2;
console.log(remainder); // prints -> 1, where 5 / 2 = 2 remainder 1.
//Let's try it again.
var leftoverHotdog = 25 % 5;
console.log(leftoverHotdog); // prints -> 0, which is probably for the best.
var x = 10;
x %= 5; // same as x = x % 5
console.log(x); // prints -> 0
/*
3. Comparison: The world of greater than, less than and equal to. Comparison operators compare two values and evaluate as true or false.
3a. == equal to
*/
console.log(1 == 1); // prints -> true
console.log(1 == '1'); // prints -> true
/*
=== strictly equal, meaning the values are also of equal type (string, number, etc.). It's best practice to use === unless you have a very good reason not to.
*/
console.log(1 === 1); // prints -> true
console.log(1 === '1'); // prints -> false because 1 is a number while '1' is a string.
/*
!= not equal
*/
console.log(2 != 5); // prints -> true
console.log(5 != 5); // prints -> false
/*
!== strictly not equal (not equal value and not equal type)
*/
console.log(2 !== 5); // prints -> true
console.log(5 !== 5); // prints -> false. This is false because the values are both 5 and are both numbers. 5 does equal 5!
console.log(5 !== '5'); // prints -> true. This is true because the values, while both 5, are a number and a string, respectively. 5 does not equal '5'!
/*
> greater than, < less than, >= greater than or equal to, <= less than or equal to
*/
console.log(100 > 1); // prints -> true
console.log(7 > 9); // prints -> false
console.log(1 < 100); // prints -> true
console.log(9 < 7); // prints -> false
console.log(9 <= 9); // prints -> true
// You get the point.
/*
4. Logical
&& and
|| or
*/
console.log(8 === 8 && 2 === 2); // prints -> true
console.log(67 > 3 || 9 < 0); // prints -> true
/*
5. ! Bang
! bang operator
! is the weird one in this. ! stands for "not". It's used in if statements to check the truthiness of a value.
*/
console.log(!true); // Prints false. read "not true = false" and it makes sense.
/*
6. typeof: Tells you the type of value
*/
console.log(typeof 2); // prints "number"
console.log(typeof 'blossom'); // prints "string"
/*
Ternary
Unary operators work with one value, binary operators work with two values, and ternary operators work with three values.
Comparisons and arithmetic operators are binary, for example. variable++, variable--, !, etc, are unary operators.
Ternary operators are the only operators that work with three values.
The syntax is (condition ? expression1 : expression2).
If true, the operator returns true, or expression 1.
Otherwise, it returns false, or expression2.
*/
var dog = 9;
var tooManyDogs = (dog > 2 ? "yes" : "no");
console.log(tooManyDogs); // prints -> yes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment