Now, why is knowing this important? It is important because you will be working with values a whole lot. Working with them in a way that doesn't drive you insane is a good thing. There are two things you need to simplify your life working with values. You need to:
- Easily identify them
- Reuse them throughout your application without unnecessarily duplicating the value itself
Those two things are provided by what we are going to be spend the rest of our time on: variables. Let's learn all about them here.
A variable is basically an identifier for a value. Instead of typing hello, world! everytime you want to use that phrase in your application, you can assign that phrase to a variable and use that variable whenever you need to use hello, world! again. This will make more sense in a few moments - I promise!
The way to use variables is by using the var keyword followed by the name you want to give your variable:
In this line of code, I declared a variable called myText. Right now, your variable has simply been declared. It doesn't contain anything of value. It is merely an empty shell.
Let's fix that by initializing our variable to a value like...let's say...hello, world!:
var myText = "hello, world!";
You probably shouldn't be too surprised that I ended up using that phrase haha. Anyway, let's modify our original example that you saw earlier to use our newly declared (and initialized) variable:
<script> var myText = "hello, world!"; alert(myText); </script>
Notice that I am no longer passing in my hello, world! text to the alert function directly. Instead, I am passing in my variable name myText instead. The end result is the same. When this script runs, an alert with hello, world! will be shown.
What this change does allow me to do is have one place in my code where hello, world! is being specified. If I wanted to change hello, world! to The dog ate my homework!, all I would have to do is just make one change to the phrase specified by the myText variable:
var myText = "The dog ate my homework!"; alert(myText);
Throughout your code, wherever you referenced the myText variable, you will now see the new text appear. While this is hard to imagine for something as simple as what we have right now, for larger applications, this convenience with having just one location where you can make a change that gets reflected everywhere is a major time saver. You'll see more, less trivial cases of the value (ha!) variables provide in subsequent examples.
More Variable Stuff
Basically, keep the following things in mind when naming them:
- Your variables can be as short one character, or they can be as long as you want - think thousands and thousands...and thousands of characters.
- Your variables can start with a letter, underscore, or the $ character. They can't start with a number.
- Outside of the first character, your variables can be made up of any combination of letters, underscores, numbers, and $ characters. You can also mix and match lowercase and uppercase to your heart's content.
- Spaces are not allowed.
I've posted some examples of valid variable names below:
var myText; var $; var r8; var _counter; var $field; var thisIsALongVariableName_butItCouldBeLonger; var __$abc; var OldSchoolNamingScheme;
Outside of valid names, there are other things to focus on as well. In the near future, I will touch upon those other things such as naming conventions and how many people commonly name variables and other things that you identify with a name.
More on Declaring and Initializing Variables
myText = "hello, world!"; alert(myText);
Notice the myText variable is being used without formally being declared with the var keyword. While not recommended, this is completely fine. The end result is that you have a variable called myText. The only thing is that, by declaring a variable this way, you are declaring it globally. Don't worry if the last sentence makes no sense. We'll look at what "globally" means when talking about Variable Scope.
One more thing to call out. The thing you should know is that the declaration and initialization of a variable does not have to be part of the same statement (fancy way of saying line). You can break it up across multiple statements:
var myText; myText = "hello, world!"; alert(myText);
In practice, you will find yourself breaking up your declaration and initialization of variables all the time.
One more thing to call out...yes, this time I am not lying. You can change the value of a variable whenever you want to whatever you want:
var myText; myText = "hello, world!"; myText = 99; myText = 4 * 10; myText = true; myText = undefined; alert(myText);
Jump Ahead: Variable Scoping
Now that you know how to declare and initialize variables, a very important topic is that of visibility. You need to know when and where a variable you declared can actually be used in your code. The catch-all phrase for this is known as variable scope.
That's all there is to the basics of declaring and using variables. I initially tried to elegantly combine variables, functions, and variable scoping into one tutorial, but I failed. I only covered variables and didn't even bother covering functions and variable scoping. Please don't tell my Asian parents that.
If you have questions, need some assistance on this topic, or just want to chat - post in the comments below or drop by our friendly forums (where you have a lot more formatting options) and post your question. There are a lot of knowledgeable and witty people who would be happy to help you out
Did you enjoy reading this and found it useful? If so, please share it with your friends:
I like to talk a lot - A WHOLE LOT. When I'm not talking, I've been known to write the occasional English word. You can learn more about me by going here.