Helping ordinary people create extraordinary websites!

JavaScript Variables

Data types

In the previous section, JavaScript Syntax, we looked at a variable set to a string. We call a string a data type, as it is a type of information that a variable can hold. But variables can hold so much more. Here are some examples of variables being assigned different datatypes:

'Sometext' A string value
105 An integer value
true A boolean value

Remember, only strings, or snippets of text, need to be quoted. You can also use numbers with decimals; these are called float values. You can assign any variable to any of these values, like this:

somevariable = 'Some text';
someothervariable = 105;
athirdvariable = 12.10;
ourfinalvariable = true;

Creating variables

Variable names must begin with a letter or an underscore, and can have letters (both capital and lowercase), numbers and underscores. Variable names are case sensitive: myvar is not the same as myVar. When you assign a variable a value, the variable is automatically created.

You should also use the var keyword when first creating a variable, like so:

var myName = "Josh";
var has_happened = true;

With the var keyword, you can also create multiple variables at the same time:

var myName = "Josh", has_happened = true;

Now, let's take a look at JavaScript functions.

« JavaScript Syntax JavaScript Functions »


More JavaScript Tutorials:
» Limiting Text In Textarea Form Field
» How to Setup a Randomising Function
» JavaScript Double Click Trapper
» Enumerating JavaScript Objects
» JavaScript Debugging Techniques with Firebug
» Pre-Fill Forms From Last Use


GET OUR NEWSLETTERS