Posted By:
26 Aug, 2014 10:48 am PDT

Variables in JavaScript

  • Please explain the concept of variables in javascript.
  • Does different keyword exist for every datatype?

 

Attachment:None


JavaScript Variables

Before you use a variable in a JavaScript program, you must declare it. Variables are declared with the var keyword as follows:

<script type="text/javascript">

<!--

var money;

var name;

//-->

</script>
 

You can also declare multiple variables with the same var keyword as follows:

<script type="text/javascript">

<!--

var money, name;

//-->

</script>
 

Storing a value in a variable is called variable initialization. You can do variable initialization at the time of variable creation or later point in time when you need that variable as follows:

For instance, you might create a variable named money and assign the value 2000.50 to it later. For another variable you can assign a value the time of initialization as follows:

<script type="text/javascript">

<!--

var name = "Ali";

var money;

money = 2000.50;

//-->

</script>
 

JavaScript Variable Names:

While naming your variables in JavaScript keep following rules in mind.

  • You should not use any of the JavaScript reserved keyword as variable name. These keywords are mentioned in the next section. For example, break or boolean variable names are not valid.
  • JavaScript variable names should not start with a numeral (0-9). They must begin with a letter or the underscore character. For example, 123test is an invalid variable name but_123test is a valid one.
  • JavaScript variable names are case sensitive. For example, Name and name are two different variables.
Was it useful?  



Javascript variables:

Variables are used in javascript to store the values like other languages c,c++,java etc...The main difference between the javascript variables and other language variables is that other languages  consists of various keywords for declaring various datatypes unlike in javascript only one keyword is availble that is 'var'  keyword..In javascript to declare any type of variable we will use 'var' keyword..

Each  javascript code shoud be in between this tags..<script></script>..

<script>

var a=10;

 

if(a%2==0)

{

alert(a+" is a Even number");

}

else

{

alert(a+" is a Odd number");

}

</script> 

Above code is used to check given number is odd or evn..

The variable 'a' is store the value 10..alert is function which is used to dispaly alert box...

Javascript does't have any other keywords for declaring different data types. 


Was it useful?  




Please login to reply to this problem.



Related Problems on Fixoncloud