Introduction to Web Development Introduction to Web Development

JavaScript Variables, Variable Types, & Reserved Words

This course has been updated! We now recommend you take the Complete Intro to Web Development, v2 course.

Check out a free preview of the full Introduction to Web Development course:
The "JavaScript Variables, Variable Types, & Reserved Words" Lesson is part of the full, Introduction to Web Development course featured in this preview video. Here's what you'd learn in this lesson:

If you need to store a value or some information, you can use JavaScript variables. Nina discusses the syntax and naming of JavaScript variables. She also outlines reserved words in JavaScript that should not be used when naming variables.

Get Unlimited Access Now

Transcript from the "JavaScript Variables, Variable Types, & Reserved Words" Lesson

[00:00:00]
>> [MUSIC]

[00:00:03]
>> Nina Zakharenko: So the way that we store values in JavaScript is with variables. And you can either declare it and initialize it online, which is what you'll usually see. Or you can declare it first and then initialize it somewhere down the line. Until that value is initialized, that variable, it has no value.

[00:00:29]
>> Nina Zakharenko: And really important to consider, don't forget the var. Like right here, this var y. Don't forget that keyword when you're using a variable for the very first time. If you don't have it in there, and you use that same variable name again, you might accidentally overwrite it.

[00:00:49] And we'll talk about scope a little bit more in detail later.
>> Nina Zakharenko: So Brian mentioned some of this before how to it applies to CSS. So for CSS, classes, for example, you name them word dash word. In JavaScript, we name our variables in camelCase. So that's lowercase word and then word with an uppercase first letter is the next word, and so on and so on.

[00:01:22] Variables either start with a lowercase letter, a dollar sign, or an underscore. But make sure they don't contain any special symbols like exclamation point or pound, hashtag, whatever you wanna call it. That's not supported, and you'll get an error.
>> Nina Zakharenko: So some words are reserved by the JavaScript language.

[00:01:43] And they can't be used as variable names either. For a beginner this is kind of a weird error cuz you'll get an error and your code looks right. And you aren't really sure what's going on. But these are some pretty common words that you can't use as JavaScript variables like in or do, this, for.

[00:02:07] There's a pretty long list of them and after class you can go through it there's a link on the bottom of the slide.
>> Nina Zakharenko: So, variables can be of different types. And JavaScript is cool because it's dynamic. You don't have to let it know what type of variable is gonna be before you use it.

[00:02:28] So, strings are contained in two quotes. It's any sort of characters, numbers that you won't be using mathematically. Numbers don't have the quotes and they can be with or without the decimal. So if we go
>> Nina Zakharenko: Back into our
>> Nina Zakharenko: Console here.
>> Nina Zakharenko: Sorry, I'm trying to make it bigger, it's.

[00:03:07]
>> Nina Zakharenko: There we go.
>> Nina Zakharenko: Can everyone see that font? So, let's try to declare some variables.
>> Nina Zakharenko: So this variable is called name and it's a string because it's in quotes. If you press enter, you'll see undefined which means that running this line of code, didn't return anything.

[00:03:40] To make one more variable called age. This one's a number. So in JavaScript if you wanna see the type of variable, you can use this typeof command. And it's a special function. You're not calling it with parenthesis, which you'll see later one. So just note that this one isn't gonna look like some of the other things you're going to try today.

[00:04:09] So, if we do typeof name, we're gonna get back a string, typeof age is a number. We'll talk about functions later on but let's create an empty one let's call it myFunc.
>> Nina Zakharenko: So a totally empty function it takes no arguments if we do typeof myFunc we'll get back a function.

[00:04:54]
>> Nina Zakharenko: So another kind of variable type is a Boolean. And Booleans can have two values. True which is yes or false which is no. There are also special types, if you declare a variable and you don't set you don't put anything in it the value of that is undefined.

[00:05:21] If you want to explicitly empty a variable you can set it to null. And that'll clear out the value.