Variables

JavaScript is a very forgiving language and that includes its usage of variables. Variables are a container for unknown or known values. This means that you can change the value of a variable as many times as you wish. JavaScript variables are defined only using the word var before them.

Some Quick JavaScript Rules

  • JavaScript is case sensitive, which means “myVariable” is not the same as “myvariable”.
  • JavaScript, for the most part, ignores white space. Depending on the programmer, you might see var example = “pickles”; or you might see var example=”pickles”;. I am going to try to stay consistent with the first one.

A variable in JavaScript is simply declared by:

Example

Of course, we have only declared the variables in the example above. We have not set any value to them, which means that they are currently empty or null.

Example

In the example above, we can see that the variable stringExample is set to “This is a string”, where the string must be in quotes. The intExample is set to 7, since 7 is a integer, it shouldn’t be in quotes. The booleanExample is set to true, since true is a boolean value. (Quick Note: true and false are the only two values a boolean can be set to.)

Variable Naming

Try to keep a consistent naming scheme when naming variables. Notice how I divided the “words” by lowering the first word and capitalizing the letter of the next word. Also, try to name variables with a sense of what their purpose is. An example would be var numberOfTurtles = 7.

References

If you enjoyed this resource, support me by sharing this page with others.
Share on Facebook
Facebook
Share on Google+
Google+
Share on StumbleUpon
StumbleUpon
Digg this
Digg
Tweet about this on Twitter
Twitter
Share on Reddit
Reddit

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.