Sams Teach Yourself JavaScript in 24 Hours

Sams Teach Yourself JavaScript in 24 Hours

By Michael Moncur

Data Types in JavaScript

In some computer languages, you have to specify the type of data a variable will store: for example, a number or a string. In JavaScript, you don't need to specify a data type in most cases. However, you should know the types of data JavaScript can deal with.

These are the basic JavaScript data types:

Although JavaScript keeps track of the data type currently stored in each variable, it doesn't restrict you from changing types midstream. For example, suppose you declared a variable by assigning it a value:

total = 31;

This statement declares a variable called total and assigns it the value of 31. This is a numeric variable. Now suppose you changed the value of total:

total = "albatross";

This assigns a string value to total. JavaScript will not display an error when this statement executes; it's perfectly valid, although it's probably not a very useful total.

Share ThisShare This

Informit Network