JavaScript Variables and Operators

Types of Variables

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.

go to previous section go to next section 


This article written by Stephen Chapman, Felgall Pty Ltd.

go to top

FaceBook Follow
Twitter Follow