|
|
Variables Tutorials > JavaScript > Variables
What are Variables?
Variables are named containers for storing small amounts of data - for example, a number or a line of text.
Declaring and Accessing Variables
Variables in JavaScript are usually declared (created) with the var keyword. They may be given a value at this time, or left undefined to be used later. Variables do not have to be declared before they are used - JavaScript will declare them itself the first time they are used. Variable names must start with a letter or an underscore, and can contain letters, numbers, and underscores. Remember that JavaScript is case-sensitive, so variables with different capitalization are considered different variables. Some examples:
var x; //Declaring a variable but leaving it undefined (no value). var y=5; //Declaring and defining a variable in one statement. z=10; //Using a variable without having declared it - this is done automatically. var 2ndVariable; //This is not valid, as variable names cannot start with a number. var myVariable = "Hello"; //A variable containing a string (text) value. var myvariable = "World"; //Yes, this is a different variable, since the 'v' is lower-case! Page Responses
Currently there have been no responses to this page...
If you have anything to contribute to this tutorial, found a bug, or know a better way of achieving the same goal, please leave your response below.
|