Guide to JavaScript Variables
A variable is a way to store information so that you can later use and manipulate it. For example, imagine a JavaScript-based pinball game where the goal is to get the highest score. When the player first starts the game, their score will be zero, but as they hit targets with the pinball, the score will get bigger. In this case, the score is a variable since it starts at 0 but increases as the game goes on.
Think of a variable as a basket: you can put an item into a basket, look inside the basket, dump out the contents of the basket, or even replace what’s in the basket with something else. However, even though you might change what’s inside the basket, it still remains a basket.
Creating a Variable
Creating a variable is a two-step process that involves declaring the variable and naming it. In JavaScript to create a variable named “score”, you would type:
var score;The first part, “var”, is a JavaScript keyword that creates, or, in programming-speak, declares the variable. The second part of the statement, “score”, is the variable’s name.
What you name your variables is up to you, but there are a few rules you must follow when naming variables:
- Variable names must begin with a letter, $, or _.
In other words, you can’t begin a variable name with a number or punctuation: so “1thing” and “&thing” won’t work, but “score”, “$score”, and “_score” are fine. - Variable names can only contain letters, numbers, $, and _.
You can’t use spaces or any other special characters anywhere in the variable name: “fish&chips” and “fish and chips” aren’t legal, but “fish_n_chips” is. - Variable names are case-sensitive.
The JavaScript interpreter see uppercase and lowercase letters as distinct, so a variable name “SCORE” is different from a variable named “score”, which is also different from “sCoRE” and “Score”. - Avoid keywords.
Some words in JavaScript are specific to the language itself. “var” for example is used to create a variable, so you can’t name a variable “var”. In addition, some words, like “alert”, “document”, and “window” are considered special properties of the Web browser. You’ll end up with a JavaScript error if you try to use those words as variable names. You can find a list of some reserved words in the table below. Not all of these reserved words will cause problems in all browsers, but it’s best to stay clear of them when naming variables.
| JavaScript keywords |
Reserved for future use | Reserved for browser |
|---|---|---|
|
break case catch continue default delete do else finally for function if in instanceof new return switchthis throwtry typeofvar voidwhile with |
abstract boolean byte char class const debugger double enumexport extends final float goto implements import int interface long native package private protected public short staticsuper synchronized throws transient volatile |
alert blur closed document focus frames history innerHeight innerWidth length location navigator open outerHeight outerWidth parent screen screenX screenY statusbar window |
| Print article | This entry was posted by kpac on June 12, 2009 at 10:19 am, and is filed under JavaScript Tutorials. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |
