Locked lesson.
About this lesson
How to store data in variables in JavaScript.
Exercise files
Download this lesson’s related exercise files.
Variables.docx58.6 KB Variables - Solution.docx
58.8 KB
Quick reference
Variables
Variables are used to store things.
When to use
You'll use variables ALL the time. Anytime you need to store data, you'll use variables.
Instructions
To create a variable, you need to declare it using the var keyword:
var myVariable;
To add things to variables, use the equal to sign:
myVariable = "Something";
You can declare and assign at the same time:
var myVariable = "Something";
Variable names can include letters, numbers, dollar signs, and underscores; but not dashes (-).
Hints & tips
- Variables are used to store data.
- Declare them first, then add stuff to them.
- 00:05 In this video I want to talk about variables.
- 00:07 Now we already looked at variables very quickly in the last video, but
- 00:10 now we can talk about them in a little bit more detail.
- 00:12 So what exactly is a variable?
- 00:14 A variable is just about the most basic thing in any programming language.
- 00:18 I mean, all programming languages have variables.
- 00:20 They're pretty universal.
- 00:21 So think of a variable as a bucket or a box.
- 00:24 You can put things in a variable.
- 00:26 You can put things in a bucket or a box.
- 00:28 It's a container that holds things.
- 00:30 You generally put just one thing in a variable at a time.
- 00:33 You can take that thing out later, do something with it, you can remove it,
- 00:36 add something else in there later on, whatever you want.
- 00:39 So how do we create a variable with JavaScript?
- 00:41 Well let's create our little script tag here.
- 00:45 The first thing we do is we declare the variable.
- 00:47 And we do that by this V-A-R that stands for variable.
- 00:51 And next we just give it a name.
- 00:52 So call it myVariable, whatever you want.
- 00:55 And you've now created a variable.
- 00:58 So you wanna be as descriptive as possible when you name your variable.
- 01:02 And like I said earlier, the convention is to use lowercase Camel Case.
- 01:06 So the first word of your name is lowercase and
- 01:09 then the next word is uppercase.
- 01:11 So like I said, try and be as descriptive as possible.
- 01:13 So if you have a variable that's gonna contain a person's full name for instance.
- 01:17 You might wanna call that one fullName, or something like that.
- 01:23 So, whatever's going to be in there, try to name the variable as that.
- 01:26 That's sort of the convention that most people try and follow.
- 01:29 And that's just so later when you're reading through your code
- 01:32 it seems obvious what each variable is.
- 01:34 So we've declared this variable, now we can assign something to it.
- 01:38 So let's change this to firstName.
- 01:44 So to assign something to a variable we've already looked at we just go
- 01:47 firstName = "John".
- 01:53 Very simple, very straightforward.
- 01:54 Now, this is sort of a two step process.
- 01:56 We declared it first, and then we put something inside of it.
- 02:00 You can do them both at the same time.
- 02:02 You can go var firstName = "John".
- 02:08 Most of the time, you'll do it this way, all at one time.
- 02:12 But there are definitely instances where you'll wanna split it up into two
- 02:15 different things.
- 02:16 Either way, it doesn't really matter which one you do, they both work perfectly well.
- 02:19 So we have declared this and added something to it.
- 02:23 We can also declare it again later on.
- 02:26 We can go var firstName like that.
- 02:31 Now even though we've put John in our variable firstName, we can go ahead and
- 02:35 declare it later on.
- 02:36 And John is still in our variable, right?
- 02:40 We don't actually delete anything by declaring it again.
- 02:42 Now you probably won't ever do this, it's just sort of one of those quirky things
- 02:45 about variables I wanted to mention since we're talking about them.
- 02:48 So what sort of things can you put in a variable?
- 02:50 Well, we've already seen that you could put in strings.
- 02:53 For instance this John right here, that's a string.
- 02:55 Likewise if you put your full name, John Elder, that's a string.
- 02:58 Any sort of text surrounded by quotes is a string.
- 03:01 You can also put numbers in there, so we could do 41, right?
- 03:06 Now that we have a number in this variable, we can do math to that variable.
- 03:09 And we're gonna talk about math later on.
- 03:11 But one thing I want to mention there's a difference between this, and
- 03:16 let's copy this.
- 03:17 And this, well actually let's not copy, let's just do it right here.
- 03:23 So this, it looks like 41 but its actually not 41.
- 03:26 This is no longer a number,
- 03:28 we've turned this into a string by surrounding it in quotation marks.
- 03:31 So we cannot do math to this right now, right?
- 03:35 Because it would be like adding two strings, like apples and oranges,
- 03:38 you can't add those.
- 03:39 A number is not surrounded by quotes, so just another little thing to keep in mind.
- 03:44 So, finally naming variables, variable names are called identifiers,
- 03:47 basically just names, right?
- 03:49 And so like I said, you want them to descriptive.
- 03:51 Names can contain letters, numbers, underscores and dollar signs but not
- 03:56 dashes because JavaScript thinks that's a subtraction sign so you can't do that.
- 04:01 You wanna try and start your variables with a letter.
- 04:04 You can start them with an underscore, like that,
- 04:09 or even a dollar sign, but that's not really done very often.
- 04:14 People just don't do that, that's just the convention.
- 04:16 So there are also some words that you can't use as variable names.
- 04:19 For instance, JavaScript keywords, and we'll talk about those a little bit later.
- 04:22 But like for instance this var, V-A-R.
- 04:25 That's what we use to declare a variable.
- 04:27 You couldn't declare a variable named var, right.
- 04:30 You would get an error if you tried to do that.
- 04:31 And there are a bunch of other words that you can't use that are sort of reserved
- 04:34 for JavaScript.
- 04:35 And you'll learn those as you go along.
- 04:37 And for the most part you're not gonna accidentally use one of those, probably.
- 04:41 But if you do you'll get an error and you'll see that.
- 04:43 So that's variables, very basic, very simple.
- 04:46 And in the next video we'll look at assignment operators.
Lesson notes are only available for subscribers.