Weeb tutorials feedback Powered by

Javascript Variables

What is a variable?

In programming a variable is used to store some form of data. We can assign a name to a variable and then assign a value to it.

A variable can have the following values assigned to it:

  • String - Simply a set of characters, such as a sentence or word.
  • Boolean - True, or false.
  • Numeric
  • Null - Basically, meaning nothing. No value.

Declaring a variable in Javascript

To declare a variable in Javascript we use the var statement. The var statement requires a name and a value.

Lets see an example of a variable declaration:

 

Javascript Variable:

<script type="text/javascript">

//Variable with numeric value:

var x=5;

// Variable with string value:

var name="John";

// Variable with boolean value:

var test=true;

// Variable with null value:

var name=Null;

 

</script>

 

 

So what use are variables?

Using operators we can do all kinds of things with our variables. This includes addition, subtraction, multiplication,division and more!

You can read more about this in the operators section.

Things to remember

  1. Variables, like everything in Javascript are case-sensitive. Remember this.
  2. You can actually declare a variable without assigning a value to it.
  3. The value of a variable can change throughout the execution of a script.
This is the old version of our site. To visit the new version click the following link: New site