-
Notifications
You must be signed in to change notification settings - Fork 4
Variables
A variable is just a name for some data.
That's it?!? Yep!
Making variables is called declaration and it looks like this:
// ┌ First we need var
// ⇣ ┌ Then a name
var sheena;
But, that doesn't have any data, just a name! Correct! Let's give sheena
some data. This is called assignment and to do it we use the assignment operator. The assignment operator to non-programmers is called the equals sign and we use it to assign a variable to some data like so:
var sheena;
// ┌ 1st comes our variable name on the left
// ⇣ ┌ Then the assignment operator
sheena = "a punk rocker";
// └ Then some data
It is very common to combine these two steps into one like so:
// Try to answer,
// what are the 4 parts of this variable declaration and assignment called?
// 1 2 3 4
// ⇣ ⇣ ⇣ ⇣
var sheena = "a punk rocker";
This is called reassignment and we again use the assignment operator:
// Here we declare a variable and assign it to some data
var clarkKent = "a mild mannered news reporter";
// After a cry for help we reassign the variable
clarkKent = "the man of steel";
In Javascript a variable can be assigned to any kind of data!! In programming a kind of data is referred to as a Datatype.
var theEarthIsRound = true;
var elephantsDont = ["swim", "drive", "wear pants"];
var myFavoriteAnimal = "jackalope";
var clownsThatCanFitInASinglePhoneBooth = 64;
// and so on
Here are some rules:
- A variable name must be at least 1 character long.
- The first character must be a letter -or- _ -or- $.
- Any characters after that can also be numbers
- A variable name cannot contain any spaces!
Here are some guidelines:
- start your variable with a lowercase letter
- use Camel Case
- use descriptive but short names
Some examples:
var x = "marks the spot";
var batman2 = "the dark knight";
var numberOfStudents = 9;
var ___z___$504___ = "i'm a rediculous variable name";
For those that want a visual description of valid variable names:
Variable names are case sensitive! That means that lower case letters and uppercase letters are not interchangeable! For example:
// These two variables are not the same!
// ┌ This one starts with a lower case letter
var halleBotStatus;
// ┌ This one starts with an upper case letter
var HalleBotStatus;
You just use their name! Since variables are assigned to data, when we use their names it's just like we're typing out all the data.
// Here we create a variable an assign it to some text
var drEvilLifeStory = "My father was a relentlessly self-improving boulangerie owner from Belgium with low grade narcolepsy and a penchant for buggery. My childhood was typical, summers in Rangoon, luge lessons. In the spring we'd make meat helmets.";
// Now we don't have to type that text anymore,
// we can just use the variable by name!
// Here we are using the text multiple times by just using the variable
tellUsAboutYourself( drEvilLifeStory );
imSorryIDidntHearYou( drEvilLifeStory );
couldYouRepeatThat( drEvilLifeStory );
itsTooLoudInHereLetsGoOutsideWhereICanHearYou( drEvilLifeStory );
Nothing!! Variables are just names for data! 👍