-
-
Save gonewayword/9abe88541c765638cdedf5aa0d7e1417 to your computer and use it in GitHub Desktop.
Javascript OperatorrrrsssDescribing the shit out of Javascript Operators// source http://jsbin.com/juquze
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//JAVASCRIPT OPERATORS UNITE | |
//Operators are sort of like the prepositions of javascript. They express a | |
//relation of one thing to another, often a variable to another variable, or they | |
//facilitate a definition. Let's take a look below at some specific operator definitions | |
//and their examples. | |
//1. Assignment | |
//Assignment operators do what their name says--they assign a meaning, definition or | |
//function to something. Assignment operators always include an equal-sign ( = ) | |
//and the only other form they take is the addition of any of the arithmetic operators | |
//before the equal sign to add that functionality to the equation. | |
//Here's a basic assignment operator in action: | |
"use strict"; | |
var brendan = 10 + 19; | |
console.log(brendan); | |
//Here's an example of an arithmetic operator added to an equal sign to make it into an arithmetic | |
//assignment operator: | |
var avery = 10; | |
avery += 18; | |
console.log(avery); | |
//2. Arithmetic | |
//Arithmetic operators are all the ways in which we can apply math to javascript variables or functions. | |
// Your basic operators are intuitive: + adds, - subtracts, * multiplies, and / divides. | |
//Modulus, or %, is a more complex arithmetic operator: it returns the remainder of the number before the | |
//modulus symbol, after being divided by the number after it. See below. | |
console.log(15 % 10); | |
//This would return 5, because 15 divided by 10 leaves a remainder of 5. The modulus is useful for | |
//discovering if a number, or a series of numbers, is divisible by another, by seeing if the modulus | |
//returns 0. | |
//Finally, the two arithmetic operators ++ and -- assign incrementation or decrementation to a number. | |
//console.log(10++); would return, for example, 10 11 12 13...but let's not do that, cause it would go | |
//on infinitely. | |
//3. Comparison | |
//Comparison operators are used for comparisons of all sorts | |
//4. Logical operators | |
//Logical operators work just like in real logical equations. | |
//&& means and, and || means or. || in javascript is an inclusive or, just like in logic, meaning | |
//if you have an if (this || this), it will qualify if BOTH conditions are met as well. This is worth | |
//noting because in common everyday speech we very often mean an exclusive or--as in, I'm going to drive | |
//or walk to the store. Occasionally, we use it inclusively: I hope the Saints win this game, or win the | |
//whole thing, superbowl and all. In the latter case, we clearly mean "this OR that OR both." | |
// If we are writing an if else statement and we want an OR to be exclusive, we have to separate the | |
//two sides from each other, as shown below: | |
var x = 8; | |
if (x != 10 || z != 10) { | |
console.log("What's down this hole? Only one way to find oooouuuuuuu"); | |
} | |
//5. Binary (!, typeOf, -) | |
// The operator ! meants not. This is useful for lots of things, most of all eliminating possibilities, | |
//rather than having to go through the process of getting a positive ID. For an example it can be as | |
//simple as: | |
var wayneDelCorral = 25; | |
function birthday(wayne) { | |
if (wayne != 25) { | |
console.log("Happy Birthday!"); | |
} else { | |
console.log("Just another day :("); | |
} | |
} | |
birthday(wayneDelCorral); | |
wayneDelCorral = 24; | |
birthday(wayneDelCorral); | |
//The typeof operator is a way of assessing what kind of data type you are dealing with. It's very useful | |
//if, let's say, you've got a program filled with many many lines of code and at a certain point you're | |
//unsure what something is...whether it's a variable you've declared as a number, a string, or etc. Then | |
//you might use the typeof operator, like so: | |
console.log(typeof wayneDelCorral); | |
//Even easier is simply typing typeof wayneDelCorral into the console and it will respond with the | |
//string "number". | |
//The negative binary operator makes something into the opposite of itself. This is only possible for | |
//certain things, | |
//6. Turnary (a ? b : c) | |
//The ternary operator requires three different operating inputs and it is used as a shorthand method | |
//of doing an if statement. It fundamentally works in the same way except you are restricted to two | |
//options because there are two possible results. It asks: is this condition true? If so, do expression 1, | |
//if not, do expression 2. As such: | |
var isVegetarian = true; | |
var meatOrVeggie = isVegetarian ? "Three Cheese" : "Pepperoni"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment