Skip to content

Instantly share code, notes, and snippets.

@gonewayword
Forked from anonymous/index.html
Last active August 29, 2016 00:40
Show Gist options
  • Save gonewayword/bcee37b0f9996165769cedb59fc94e12 to your computer and use it in GitHub Desktop.
Save gonewayword/bcee37b0f9996165769cedb59fc94e12 to your computer and use it in GitHub Desktop.
VariablesDescribing variables in detail.// source http://jsbin.com/bixeloc
//1. Variables are sort of metaphorically like defining a character
//with whom your program, the author, is going to play. Variables
//are for storing the definition of something in memory. When you
//declare a variable you are basically creating a tool for your
//program to hold on to and be able to work with, and naming that tool.
//They can be many different types of things, including a number, a string,
//a boolean, a function, etc. They're called variable because they are
//changeable throughout the program.
//2. To declare a variable you use the term "var", then the name (or ID) of
//the variable, then and equals sign, and then the content of that variable,
//whatever it may be. The ID of a variable should be in camel case, which
//means that the first letter should be lower case, and if the ID has multiple
//words, any words after the first should begin with a capital letter.
var currentMood = "stymied";
//3. Variables can perform many tasks as tools, but only if we incorporate them in
//some way in the program. For example, if we console.log (this "logs" to console,
//or prints out) a variable, and have declared it correctly, the variable will
//then appear in our console. If we call a variable as the input of a function,
//then the variable will pass through the function, and participate in that
//function based on its commands.
function brendan(mood) {
if (mood === "stymied") {
console.log("Have a beer")
}
else if (mood === "bored") {
console.log("Do some coding")
}
else {
console.log("What you smokin")
}
}
brendan(currentMood);
//4. Multiple variable declarations and the necessity of deliberate "var"s
//There are three types of prefixes used to declare a variable: var, const, and let. Var is
//the old standard. A variable can be redefined later in the program, as long as it's not a "const," and no prefix is
//required for declaring a variable. For example:
var brendan2 = "galoot";
console.log(brendan2);
const jeremiahReligion = "amish";
console.log("What religion is Jeremiah? Well, he's" + " " + jeremiahReligion);
let marmaduke = "conqueror";
console.log(marmaduke);
marmaduke = "terrible conqueror";
console.log(marmaduke);
//It is very important to think about how you are declaring variables, as each way has
//its own attributes that can make your program behave differently. For one example, if
//you are attempting a for loop or any other sort of variable inside of a function, and you
//do "i = 0" or "terra = form", that is, if you forget to include "var" or another prefix,
//this makes your new variable global, and can mess with your whole plan. In such a case,
//it's crucial to include a prefix.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment