Home

How to Store Data in JavaScript Variables

|
|  Updated:  
2016-03-26 07:26:16
JavaScript Essentials For Dummies
Explore Book
Buy On Amazon

After you've created a variable in JavaScript, you can store any sort of data inside it. When the data is in there, you can recall it at any time. Try it out!

  1. Open the JavaScript Console in Chrome.

  2. Create a new variable named book by typing the following and then pressing Return (Mac) or Enter (Windows):

    var book;

    You've created your container, or variable, and named it "book."

    When you press Return or Enter, the JavaScript Console displays the word undefined. This is exactly what you want to happen. JavaScript is just telling you that your code ran correctly and that it doesn't have anything to tell you.

    It may seem funny that JavaScript tells you that it has nothing to tell you. But, it's way better that it says something, even if it's just undefined than if it were to give you the cold shoulder and say nothing at all.

  3. Put a value into your new variable by typing the following code.

    book = "JavaScript For Kids For Dummies";

    You've now put data inside your variable, where it will be stored.

    When you press Return or Enter, JavaScript responds with the name of the book.

    You only need to type var when you first create and name your variable. When you want to change the data inside your variable, you only need to use the variable's name.

  4. Now, temporarily forget the name of the book. Got it? Now, imagine that you need to recall the name of the book so that you can tell your friend about it! To recall the data, or value, in a variable, you can just type the name of the variable in the console. So, type the following:

    book

    The console recalls the string that was assigned to the book variable and prints it out, as shown here.

    Printing out the value assigned to a variable.
    Printing out the value assigned to a variable.

    Notice that you didn't use a semicolon (;) when typing — you just used a variable name in the JavaScript Console. The name of a variable isn't a full JavaScript statement, so it doesn't require a semicolon. You're just asking JavaScript for the value of the variable, just as if you had asked it 1 + 1.

  5. Now try changing the value of the book variable by typing the following statement into the JavaScript Console:

    book = "The Call of the Wild";
  6. Type book into the JavaScript Console to retrieve its new value.

    The console prints out "The Call of the Wild" (or whatever you entered as the new value of book.

In addition to text, variables can also hold several other different types of data.

The data inside a variable can also be called the value of a variable.

About This Article

This article is from the book: 

About the book author:

Chris Minnick is an accomplished author, teacher, and programmer. Minnick authored or co-authored over 20 books, including titles in the For Dummies series. He has developed video courses for top online training platforms and he teaches programming and machine learning to professional developers at some of the largest global companies.

Eva Holland is an experienced web developer, tech trainer, and coauthor of Coding with JavaScript For Dummies. She is a co-founder of WatzThis?, a company focused on training and course development.