Menu Close

JavaScript Variables

In this tutorial, you will learn about JavaScript variables. As you know that, variables are the most important part of any programming language.
The meaning of variables is the same in all the programming languages. Basically, variables in programming languages are used to store some values for later use.

In this JavaScript variables tutorial, we will cover all about variables and its usages.

JavaScript Variables

JavaScript variables are containers for storing some values for further usages.

Why use JavaScript variables ?

Variables in programming languages play most importamt role, sometimes we want to use same value throughout the javascript code, Then variable will be best option of storing that value.


We can create a any number of variables in out javascript code but you have to keep some rules in your mind during the creation of variables.

Example:

var x, y, z;
x = 34;
y = "programmingfunda";
z = 12.0;

In above example we have declared three variable x,y and z using var keyword.

x store the value 34.
y store the value 'programmingfunda'.
z store the value 12.0.

JavaScript Identifiers

In JavaScript, all the variables must be identified with unique names. Identifiers can be a short name like x and y or must be a descriptive names like firstName or lastName.

Some important rules for creating JavaScript variables.

  • Name can contain a letter, digits, underscore, and Doller sign.
  • The name must begin with a letter.
  • Name can also begin with $ and _.
  • Name must be unique (firstName and firstname are different variables).
  • The reversed word that means JavaScript keywords can not be used as variable.

Declare JavaScript Variables

The process of creating a variable in javascript is called declare a variable. To create a variable in javascript you need to use the var keyword.

Syntax

var variable_name;

Example

var firstName;

After the declaration of the variable with no value, technically it has the value undefined.

To assign the value to the variable, use equal ( = ) sign.

firstName =  'programmingfunda';

In above example we have create a variable firstName and assign value programmingfunda.

Declare many variables in One statement

JavaScript provide a facility to create many variables in just one statement.

var firstName = 'programming', lastName = 'funda';

Re-declare JavaScript Variables

If you are re-declare JavaScript variable.it will not lose its value.

<html>
<head>
</head>
<h2>JavaScript Statement</h2>
<body>
<p id="myID"></p>
<script>
var name = 'programmingfunda';
var name;
document.getElementById("myID").innerHTML = name;
</script>

</body>
</html>

JavaScript Doller sign ($)

You can start JavaScript variable with Doller sign ( $ ).

<html>
<head>
</head>

<h2>JavaScript Statement</h2>

<body>
<p id="myID"></p>
<script>
var $name = 'programmingfunda';
document.getElementById("myID").innerHTML = $name;
</script>
</body>
</html>

JavaScript underscore (_)

You can start JavaScript variable with underscore (_).

<html>
<head>
</head>
<h2>JavaScript Statement</h2>
<body>
<p id="myID"></p>
<script>
var _name = 'www.programmingfunda.com';
document.getElementById("myID").innerHTML = _name;
</script>
</body>
</html>

JavaScript Arithmetic

You can do arithmetic operation with JavaScript variables with the help of =,+,,*, etc.

<html>
<head>
</head>
<h2>JavaScript Statement</h2>
<body>
<p id="myID"></p>
<script>
var value = 12 + 20 - 10 * 23;
document.getElementById("myID").innerHTML = value;
</script>
</body>
</html>

Conclusion

In this article, you have learned all about JavaScript variables along with examples. JavaScript variables are most important to store value for further use.

I think you wouldn’t have any confusion regarding JavaScript variables after this article. If you like this article, please share and keep visiting for further JavaScript tutorials.

Reference:- Click Here

Previous Article
Next Article

Was this post helpful?

Related Posts