var x; var myVar; var counter1;
var 1stCounter; var new variable; var new.variable; var var;
var x, y, zeta;
These can be initialized on the same line, too:
var x = 1, y = "hello", zeta = 14;
We’ll be covering the following topics in this tutorial:
Make names descriptive. You probably shouldn’t use a name such as r or x, because later it will be hard to remember exactly what the variable is supposed to contain. Names such as taxRate or greeting will make your code much easier to follow. Don’t use spaces or punctuation. Most languages don’t allow multiword variable names.
Many programmers use capitalization (taxRate) or underscore characters (tax_rate) to make multiple word variable names easier to read. Many of the punctuation characters have special meanings, so it’s not a good idea to put these in a variable name.
Don’t make your variable names so long that they are impossible to type correctly. Many programmers prefer variable names from 5 to 15 characters long.
Take a look at this line from the Hello Joe program:
greeting = "Hi there, Joe";
The equals sign (=) indicates assignment. It might make more sense to read this statement as follows: greeting gets the string literal “Hi there, Joe”.
Here are some practical examples of scoping, which you can also find in the companion code in the scope1.txt file
The code defines two variables: a global variable called aNewVariable; and a variable called incomingBits, which is local to the doSomething() function. Both variables are passed to respective alert() functions within the doSomething() function. When the doSomething() function is called, the contents of both variables are sent successfully and displayed on the screen,