How to use variables in JavaScript

In this article we will discuss about how to declare and use the valriables in JavaScript.
  • 2282

Variables are the temporary storage in which we can store any type of value whether it is of string, integer or any type of value. Before using the variables we need to declare them. In JavaScript we will use the Var keyword to declare the variables. While giving the name to the variable we should keep some things in mind like the variable name should not start with any numeric digit, it should be start with any alphabet or underscore. For example: 345cost is invalid variable name whereas cost34 or _345cost is valid variable name.

JavaScript Code:

<script type="text/javascript">

<!--

    var age;

    var firstname;

//-->

</script>

We can declare number of variables using same var keyword as follows:

<script type="text/javascript">

<!--

    var age, firstname;

//-->

</script>

We can assign values to the variables in two ways: First we can assign the values to the variables while declaring them and Secondly, first declare the variable and later then assign the value to it whenever necessary.

Lets have a look on the following JavaScript Code:

<script type="text/javascript">

<!--

    var firstname = "Maggi";

    var age;

    age = 25;

//-->

</script>


Ask Your Question 

Got a programming related question? You may want to post your question here
 
© 2020 DotNetHeaven. All rights reserved.