New Feature: Playlist Center! Pick a topic and let our playlists guide the way.

Start learning with our library of video tutorials taught by experts. Get started

JavaScript Essential Training
Illustration by

Creating variables


From:

JavaScript Essential Training

with Simon Allardice

Video: Creating variables

In all programs and all programming languages we have to keep track of many pieces of data, email addresses, a date of birth, position of an image on the screen, the current score in a game, so we create variables to hold that data. A variable is a container. It's us grabbing a little piece of computer memory and giving it a name, so we can use it while our JavaScript is running. We create it, we name it, we put a value in it, and then we change the value as we need to. In JavaScript, you create a variable like this.
Expand all | Collapse all
  1. 3m 28s
    1. Welcome
      1m 1s
    2. What you should know
      1m 44s
    3. Using the exercise files
      43s
  2. 15m 41s
    1. Introduction to JavaScript
      8m 6s
    2. Creating your first JavaScript
      2m 13s
    3. Getting to know the tools and applications
      5m 22s
  3. 56m 8s
    1. Understanding the structure of JavaScript code
      7m 9s
    2. Where to write your JavaScript
      3m 56s
    3. Creating variables
      6m 21s
    4. Working with conditional code
      5m 44s
    5. Working with operators
      13m 28s
    6. Sending messages to the console
      2m 59s
    7. Working with loops
      8m 1s
    8. Creating functions
      8m 30s
  4. 36m 13s
    1. Working with arrays
      7m 57s
    2. Working with numbers
      6m 13s
    3. Working with strings
      8m 27s
    4. Working with dates
      5m 38s
    5. Working with objects
      7m 58s
  5. 9m 6s
    1. What is the DOM?
      5m 49s
    2. Working with nodes and elements
      3m 17s
  6. 25m 17s
    1. Accessing DOM elements
      11m 3s
    2. Changing DOM elements
      5m 42s
    3. Creating DOM elements
      8m 32s
  7. 24m 45s
    1. Introduction to JavaScript event handling
      8m 16s
    2. Working with onClick and onLoad events
      7m 36s
    3. Working with onBlur and onFocus events
      2m 36s
    4. Working with timers
      6m 17s
  8. 21m 41s
    1. Common JavaScript errors
      7m 14s
    2. Using Firebug
      4m 7s
    3. Going through a debugging session
      10m 20s
  9. 10m 13s
    1. Accessing form elements
      4m 20s
    2. Preventing a form from being submitted
      2m 36s
    3. Hiding and showing form sections
      3m 17s
  10. 9m 49s
    1. CSS and JavaScript
      3m 46s
    2. Removing and applying CSS classes
      2m 16s
    3. Changing inline styles
      3m 47s
  11. 19m 44s
    1. Understanding JavaScript style
      7m 39s
    2. Minifying your code
      4m 28s
    3. Using JavaScript code checkers
      7m 37s
  12. 22m 24s
    1. Introduction to JavaScript libraries
      3m 17s
    2. Linking to multiple JavaScript files
      2m 11s
    3. Introduction to jQuery
      12m 7s
    4. Using a content distribution network to deliver JavaScript files
      4m 49s
  13. 17m 35s
    1. JavaScript in HTML5
      9m 37s
    2. Using Modernizr
      3m 2s
    3. Using Strict Mode
      4m 56s
  14. 33m 3s
    1. Knowing the JavaScript to avoid
      6m 35s
    2. Introduction to regular expressions
      6m 56s
    3. Working with AJAX
      10m 44s
    4. Working with objects and prototypes
      8m 48s
  15. 21m 10s
    1. Example: Countdown
      8m 3s
    2. Example: Resize
      5m 47s
    3. Example: Accordion
      7m 20s
  16. 4m 58s
    1. Where to go from here
      4m 0s
    2. Goodbye
      58s

Watch this entire course now—plus get access to every course in the library. Each course includes high-quality videos taught by expert instructors.

Become a member
please wait ...
JavaScript Essential Training
5h 31m Beginner Jul 22, 2011

Viewers: in countries Watching now:

Use JavaScript to add new features and a richer, more compelling user interface on web pages. This course keeps current best practices and practical uses for JavaScript in mind, while covering syntax, working with the DOM, and developing and debugging across multiple platforms, devices, and browsers. Author Simon Allardice also shows how to progressively enhance and gracefully degrade web pages, and take advantage of the world of JavaScript libraries now available.

Topics include:
  • Understanding the structure of JavaScript code
  • Creating variables, functions, and loops
  • Writing conditional code
  • Sending messages to the console
  • Working with different variable types and objects
  • Creating and changing DOM objects
  • Event handling
  • Working with timers
  • Debugging JavaScript
  • Building smarter forms
  • Working with CSS, HTML5, and JavaScript
  • Using regular expressions
Subjects:
Developer Web Web Design Programming Languages Web Development
Software:
JavaScript
Author:
Simon Allardice

Creating variables

In all programs and all programming languages we have to keep track of many pieces of data, email addresses, a date of birth, position of an image on the screen, the current score in a game, so we create variables to hold that data. A variable is a container. It's us grabbing a little piece of computer memory and giving it a name, so we can use it while our JavaScript is running. We create it, we name it, we put a value in it, and then we change the value as we need to. In JavaScript, you create a variable like this.

You use the word var, all written in lowercase, which is part of the JavaScript language, and then the word you want. The name of the variable itself is up to us, and it should represent the piece of data that we want to hold, in this case year, but it could be customerEmail, or todaysDate, or while we're experimenting, just nonsense words like foo, or even single letters like x. The name that you want to use for your variable must be written as one word, no spaces are allowed, and it can be made of letters, numbers, the underscore, and the Dollar sign, but you can't start with a number.

So, for example, a variable with the name 99problems would not be allowed, but a variable with the name problems99 would be allowed. Now we'll talk more about naming our variables and what you should call them later on when we talk about style in JavaScript. I'm just going to use simple names for now. So what we're doing with this line of code is we're just carving out a little area of memory to hold a value. And right now after this line var year runs, this variable exists.

It has a name, year, but it doesn't have a value. It's regarded as undefined, and undefined has a special meaning in JavaScript. Now there is no real point in having a variable that stays undefined, so we can define, or set, the initial value of the variable when we create it. We could do that as two statements: first, var year to make the variable and then year = 2011 to set it. Now the equal sign here is setting the variable to the value 2011. It is an instruction.

Now, more typically if you're doing this, you just combine these into one statement, var year = 2011, to both define it and set its value. Now here is another place where JavaScript will let you be sloppy and I won't. Technically, the word var is not even required. In JavaScript, if I write some script that starts off with this line, JavaScript will go looking for an existing variable called year, but if it doesn't find it, it will just make it.

However, we are always going to use var when defining our variables. There are a couple of situations where leaving var off can lead to unexpected behavior, so make a habit of always using it. Now variable names are case sensitive so var x with lowercase and var x with uppercase would be two different variables. Now you can run into problems accidentally sometimes here. Let's say we create a variable called x lowercase and set it equal to 200, and then later in our code we just want to change it to 210. Well, if I accidentally use an uppercase X here, I've got a problem: because of the automatic creation of variables from JavaScript, we now end up with two different ones.

And now nothing would give you an error, and nothing would inform you that this is happening. So be careful when naming your variables. Now if you're creating multiple variables at the same time, instead of spreading them across multiple lines, you can create one JavaScript statement and just separate the names of the variables with commas, and that would give you three variables here. Similarly, if you're creating and initializing them with these initial values, you can separate those with commas as well to both create them and set them.

So just by running that one line, we get an area of memory allocated for us with the name that we chose. Now, in many other programming languages when you define a variable, you don't just give it a name, but you also have to say exactly what type of information is going to be stored in each variable. Is it an integer? Is it text? Is it a Boolean value, which just means a value that's either true or false, and so on. Now once you've chosen the type you're not allowed to change it, but in JavaScript we don't do that.

JavaScript is what's called a weakly typed language. We don't make an integer variable, or a text variable, or a Boolean variable. We just use var to create a generic variable and then simply put whatever type of value we want in it. So if I executed the line var myVariable, I actually begin with this as undefined. If I then say myVariable = 200, that variable stores the number in it. If I say myVariable = Hello and the word Hello in double quotes here, this is a piece of text, what's referred to as a string, and that just means one or more characters strung together.

Now with JavaScript, you can use either double quotes or you can use single quotes to surround a string of text, but just don't mix them. Don't open with double quotes and close with single quotes. I tend to use double quotes because that's more common in other languages, but you'll see both ways. And we can store Boolean values, which is just a value that can either be true or false. Now with these you do not need the quotes around them. JavaScript understands the words true and false written in lowercase.

They are considered part of the JavaScript language. So you can start a variable off as undefined, then put a number in it, then a string in it, then a Boolean in it. That doesn't mean JavaScript doesn't care about the type of data you have--it does. It treats numbers differently from strings and strings differently from Booleans, but any JavaScript variable can hold any of these values, and more complex data too, including arrays and objects and even functions, and we're going to go deeper into how they all behave later on. And as you'll see, sometimes you'll need to ask, "Well, what kind of value is in a particular variable?" Now of course, the point of making variables is that we're going to use them.

We're going to manipulate them, ask questions of them, but that all begins by knowing how to make them.

There are currently no FAQs about JavaScript Essential Training.

 
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.
Upgrade now


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

join now Upgrade now

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed JavaScript Essential Training.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Are you sure you want to delete this note?

No

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.