|
Free Open Book
Sams Teach Yourself JavaScript in 24 Hours |
Data Types in JavaScriptIn 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, replacing the numeric value. JavaScript will not display an error when this statement executes; it's perfectly valid, although it's probably not a very useful total. By the Way Although this feature of JavaScript is convenient and powerful, it can also make it easy to make a mistake. For example, if the total variable was later used in a mathematical calculation, the result would be invalidbut JavaScript does not warn you that you've made this mistake. |
Main Menu |
| 500 Juegos Gratis | 500 Giochi Gratis | 500 Jeux Gratuits | 500 Jogos Gratis | 500 Kostenlose Spiele |