Home Javascript JavaScript Variables and Data types
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

JavaScript Variables and Data types

Advertisement
Every piece of data is known as a value. When a value is referred in a statement, it is called a literal value.

 JavaScript Variables and Data types

     

In this article you will learn the basics of JavaScript Variables and Data types.

JavaScript Variables
.Every piece of data  is known as a value. When a value is referred  in a statement, it is called a literal value. For the same reason people are identified by names as opposed to "human" or "person", literal values can be named in order to make repeated reference to them practical, efficient and readable. These names are called variables. 

Declaring and Initializing the Variables

var num   // declare variable, has null value
num = 20   // assign a value, null value is replaced
//OR
var num=20   // declare AND assign value (initialize)
var a,b,c,d //multiple variables may be declared simultaneously
var a = 5, b = 6, c = 7,d=8 //multiple variables may be initialized simultaneously

Variable Names
Variable names cannot contain spaces, begin with a number and can not be used the underscore (_) . JavaScript Variable name can not use any reserved keywords. The two words can either be separated by an underscore (my_variable) or (myVariable). 

Variable Scope
There are two types of Variable scope:- 
1. Globle scope Variable, 
2 .  Local scope Variable. 
1.Globle scope Variable:- A variable declared or initialized outside a function body has a global scope, making it accessible to all other statements within the same document.
2. Globle scope Variable:- A variable declared or initialized within a function body has a local scope, making it accessible only to statements within the same function body.
[NOTE:- If a variable is initialized inside a function body but the var keyword is left off, the variable has a global scope and is only accessible after the function containing it is invoked. It is safer to always use the var keyword ]

Java script Data type
Data Type:-
A data type is defined by the value assigned to it . A variable could be a number on one line, then assigned a string value and later be assigned an object reference. JavaScript would change its data type dynamically .It also represent the data's nature. Data is directly treated on those data types and calculate up on the works . 
There are two types of the data types, As follow:-
1.Primitive data types
2.Compositive data types
1.Primitive data types:- Primitive data types are the simplest building blocks of a program. There are following types :-

  • numeric:-JavaScript supports both integers and floating point numbers .Integers are whole numbers and it does not support the decimal numbers .Such as : 145 . Floating point numbers are fractional numbers or decimal numbers . Such as : 12.50 
  • string:- String is a collection of characters enclosed in either single quotes and double quotes .If the string starts with a single quotes and it must end with single quotes .If the string starts with a double quotes and it must end with double quotes . Such as : 'Welcome to JavaScript'
     or "Welcome to JavaScript".
  • Boolean:-Boolean literals are logical values that have only one of two values, true or false. It used to conditional sanitations. Our statements 
    are checked true or false then we use the Boolean variables.
  • null:-The null value represents no value that means strings are empty . 
  • undefined:- A variables to be declared but given no any initial value then it runtime error display  
      

Example for :-This example is represent the sum of two floating numbers and print the messages in single quotes and double quotes.

<html>
<head>
<script language="javascript">
function showAlert()
{
var a=200.40;
var b=10.50;
var sum=0;
sum=a+b;
document.write(" sum= "+sum);
document.write("\t\tHello\nworld!\n");
document.write('\nWelcome to JavaScript');
}
</script>
</head>
<body>

<script language="javascript">

showAlert();

</script>

</body>
</html>

 

This program is used to Boolean expression and it display the numbers between 1 to 500 and if user to not enter the number then it display the you
did not enter any number. 

<html> 
<body> 
<script type="text/javascript"> 
var n=0;
n=prompt("Enter a number");
document.write("Your entered number is :"+n);
if (n>=1 && n<10) 
document.write("Your entered number is greater than 1 and less than 10");
else if(n>=10 && n<20) 
document.write("Your entered number is greater than 10 and less than 20");
else if(n>=20 && n<30) 
document.write("Your entered number is greater than 20 and less than 30");
else if(n>=30 && n<40) 
document.write("Your entered number is greater than 30 and less than 40");
else if(n>=40 && n<100) 
document.write("Your entered number is greater than 40 and less than 100");
else if(n>=100 && n<=500) 
document.write("Your entered number is greater than 100 or less than 500");
else
document.write("You did not enter any number!") 
</script> 
</body> 
</html> 

 

     

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 4, 2006

Related Tutorials

Ask Questions?    Discuss: JavaScript Variables and Data types   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
SANDEEP PANDITA
May 30, 2011
mistake

Look at the section: 1. Globle scope Variable, 2. Local scope Variable. When u r defining these in this page, the heading for Local scope Variable. is wrongly written as Globle scope Variable, This looks like: 1.Globle scope Variable:- A variable declared or initialized outside a function body has a global scope, making it accessible to all other statements within the same document. 2. Globle scope Variable:- A variable declared or initialized within a function body has a local scope, making it accessible only to statements within the same function body. For both the heading/title is same.
Anirban dutta
November 9, 2011
look..

i see your [http://www.roseindia.net/javascript/variables-data-types.shtml]site..and i noticed that a your developer do a little mistake and that is [*Variable Scope There are two types of Variable scope:- 1. Globle scope Variable, 2 . Local scope Variable. 1.Globle scope Variable:- A variable declared or initialized outside a function body has a global scope, making it accessible to all other statements within the same document. 2. Globle scope Variable:- A variable declared or initialized within a function body has a local scope, making it accessible only to statements within the same function body. [*] please solve that thank you...........
Veeranjaneyulu
January 6, 2012
javascript

Their is a small small mistakes in the content of JavaScript please verify and put the correct data.
venkatreddy
February 7, 2012
javascript

in javascript tutorial mistake... in the variable scope side headings is wrongly placed for local variable scope.......
Michael Boyd
February 14, 2012
How to put this into variables

Variable declarations section: 1. Declare a variable that holds the income amount entered by the user. 2. Declare a variable that holds the minimum income that will not be charged taxes. 3. Declare a variable that holds the tax percentage for tax bracket 1. 4. Declare a variable that holds the tax percentage for tax bracket 2. 5. Declare a variable that holds the highest income for tax bracket 1. 6. Declare a variable that holds the highest income for tax bracket 2.
Michael Boyd
February 14, 2012
Write comment about these coding sections

7. Assign $1500 as the highest income amount that will not be charged taxes. 8. Assign the highest income for tax bracket 1 to be $25K and the tax percent to 15%. Anything over $25K is in the next tax bracket. 9. Assign the highest income for tax bracket 2 to be $40K and the tax percent to 20%. Anything over $40K is in the next tax bracket. 10. Ask the user to enter a dollar amount. 11. Convert the data entered into a number.
Michael Boyd
February 14, 2012
How to put this in logic and output

Logic and Output section: 12. Use only variables in your logic. 13. Determine whether or not the dollar amount entered is taxable. 14. Determine whether or not the dollar amount is in tax bracket 1 or 2. 15. Calculate the amount of tax on the dollar amount and display a message that tells the user what the tax amount would be on the number they entered. 16. For amounts greater than $40k display the message ?I do not have the data to calculate the tax on this income.
puspendra tiwari
April 13, 2012
java language

what is document.write()and what is means
Have a good name for it? Enter Here
June 28, 2012
hiii

hello
Siphiwe
July 10, 2012
What are u talking about?

What does the following sencence in your post mean? "Variable names cannot contain spaces, begin with a number and can not be used the underscore (_) ."
DMCA.com