.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
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
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
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 :-
- 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 :
- 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.
document.write(" sum= "+sum);
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.
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");
document.write("You did not enter any number!")