JavaScript Variables and Data types

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

 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>