1. Technology

JavaScript Variables and Operators

3. Types of Variables

By

We can store many different types of value in JavaScript variables, just as there are different types of coat that can be left in the restaurant cloakroom. JavaScript, unlike a number of other programming languages, even allows us to change the type of the information that is stored in a particular variable when we change its value. Some examples of different types of information that can be stored in variables include numbers, text strings, Boolean values (true and false), special values - null and undefined - as well as more advanced types such as dates and arrays.

JavaScript creates the association between a variable name and the location in memory where it stores its value the first time that you reference that variable name. Be careful when referencing your variables, the slightest difference in spelling or capitalization will result in a new variable being created rather than accessing the existing variable as you intended.

Warning: JavaScript is what is known as a weakly typed language. This means that you can store different types of information in the same variable at different times. While you might be using a particular variable to store a number at one spot in the script, you may be using the same variable to store some text later in the same script. With a strongly typed language this would generate an error condition, which you would resolve by changing one of the variable's names. JavaScript allows us to reuse the same variable for different values and leaves us to keep track of what type of information we have stored in a given variable at a given time. It is not a good idea to reuse variables like this. That JavaScript allows it just means that we have to be more careful to make sure what type of information we are processing. The same calculation may produce completely different results if, for example, a variable contains a text string and we expected it to contain a number.

This tutorial first appeared on www.felgall.com and is reproduced here with the permission of the author.

  1. About.com
  2. Technology
  3. JavaScript
  4. Javascript Tutorials
  5. Learn Modern JavaScript
  6. 1. Variables and Operators
  7. JavaScript Variables and Operators - Types of JavaScript Variable

©2014 About.com. All rights reserved.