Last active
May 6, 2017 21:50
-
-
Save joannasese/9d7fae6e35792838ea90c8b06909340f to your computer and use it in GitHub Desktop.
Variables
This file contains 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta name="description" content="Variables"> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>Variables</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript"></script></body> | |
</html> |
This file contains 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
/* | |
VARIABLES: | |
0. Like in algebra (remember those days?), variables are used in Javascript to assign a value. In Javascript, variables are | |
called as such because their values can be changed/reassigned. For example, a variable could be represented by something as simple | |
as "x" or "y" (again, like in algebra), though it would be better practice in Javascript to give your variables more pertinent names. | |
1. Declaring a variable. | |
To create a variable, use the keyword "var" and name it. This could look like: | |
*/ | |
var petName | |
/* | |
where you are "declaring" var petName. The variable name should be in camelCase. thisIsCamelCase. ThisIsNot. Noristhis. | |
When a variable is first declared, it remains undefined because it hasn't had a value assigned to it. Think of it in terms of a form at | |
the vet's office. You may see "Pet's Name:" with a blank space next to it where you're supposed to write in your dog's name. The field | |
"Pet's Name" exists, but no one's going to know what your pet's name is until you write it in. | |
When we run "var petName", it will come up as undefined. | |
*/ | |
console.log(petName); // prints -> undefined | |
/* | |
Which leads us to... | |
2. Initializing a variable (aka assigning a variable) | |
To assign a value to our variable, set the variable name to equal the desired value. The value can be any one of a variety of data types, | |
which we will discuss later. | |
Example: | |
*/ | |
petName = 'Bear'; | |
/* | |
where I have assigned "Bear" to petName. When I call petName, it will now print "Bear" | |
*/ | |
console.log(petName); //prints -> Bear | |
/* | |
But also remember that we can reassign variables. Since I've decided I no longer like the name "Bear," I am renaming by pet. Now his name | |
is Mortimer. | |
*/ | |
petName = 'Mortimer'; | |
console.log(petName); // prints -> Mortimer | |
/* | |
3. Let and Const | |
You can also declare a variable with "let" and "const." Instead of something like "var chattyCathy" you could do "let chattyCathy" or "const | |
chattyCathy." But what's the difference? | |
3a. Let | |
Let are var are almost interchangeable, but not quite. The difference is all about scope. Let is used to declare variables that are limited in scope. | |
Below is a great example from MDN that explains things pretty well: | |
*/ | |
function varTest() { | |
var x = 1; | |
if (true) { | |
var x = 2; // same variable! | |
console.log(x); // 2 | |
} | |
console.log(x); // 2 | |
} | |
function letTest() { | |
let x = 1; | |
if (true) { | |
let x = 2; // different variable | |
console.log(x); // 2 | |
} | |
console.log(x); // 1 | |
} | |
/* | |
3b. Const | |
Const declares a constant variable, which means it cannot be reassigned the way var can be. Trying to reassign a const will result in an error. | |
*/ | |
const favColor = "pink" // prints -> "pink" | |
favColor = "blue" // prints -> ERROR |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment