Uh oh!
There was an error while loading. Please reload this page.
- 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 namevarsheena;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:
varsheena;// ┌ 1st comes our variable name on the left// ⇣ ┌ Then the assignment operatorsheena="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// ⇣ ⇣ ⇣ ⇣ varsheena="a punk rocker";This is called reassignment and we again use the assignment operator:
// Here we declare a variable and assign it to some datavarclarkKent="a mild mannered news reporter";// After a cry for help we reassign the variableclarkKent="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.
vartheEarthIsRound=true;varelephantsDont=["swim","drive","wear pants"];varmyFavoriteAnimal="jackalope";varclownsThatCanFitInASinglePhoneBooth=64;// and so onHere 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:
varx="marks the spot";varbatman2="the dark knight";varnumberOfStudents=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 lettervarhalleBotStatus;// ┌ This one starts with an upper case lettervarHalleBotStatus;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 textvardrEvilLifeStory="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 variabletellUsAboutYourself(drEvilLifeStory);imSorryIDidntHearYou(drEvilLifeStory);couldYouRepeatThat(drEvilLifeStory);itsTooLoudInHereLetsGoOutsideWhereICanHearYou(drEvilLifeStory);Nothing!! Variables are just names for data! 👍