Saturday, 25 January 2014

java script data types

JavaScript Data Types
·         JavaScript variables can also hold other types of data, like text values (person="Kiran Kumar").
·         In JavaScript a text like "Kiran Kumar" is called a string.
·         When you assign a text value to a variable, put double or single quotes around the value.
·         When you assign a numeric value to a variable, do not put quotes around the value. If you put quotes around a numeric value, it will be treated as text.
Types of Datatypes:
1.    Primitive Data types
2.    User Defined Datatypes://not available in javascript

1.    Primitive Datatypes:
1.    Numbers
2.    Strings
3.    Boolean
//we have so many datatypes but we consider these 3 datatypes
Example:
var pi=3.14;
var s="Rama Rao";
var a='Hey!';

JavaScript Has Dynamic Types
JavaScript has dynamic types. This means that the same variable can be used as different types:
Example
var x;               // Now x is undefined
var x = 5;           // Now x is a Number
var x = "Ram";      // Now x is a String


JavaScript Strings
·         A string is a variable which stores a series of characters like "John Doe".
·         A string can be any text inside quotes. You can use single or double quotes:
·         You can use quotes inside a string, as long as they don't match the quotes surrounding the string:
Example
<!DOCTYPE html>
<html>
<body>
<script>
var movie="evadu";
var movie2='sri ram';
document.write(movie + "<br>")
document.write(movie2 + "<br>")
</script>
</body>
<!--http://improvejavascript.blogspot.in/  -->
</html>
JavaScript Numbers
JavaScript has only one type of numbers. Numbers can be written with, or without decimals:
Example
var x1=34.00;      // Written with decimals
var x2=34;         // Written without decimals
Extra large or extra small numbers can be written with scientific (exponential) notation:
Example
var y=123e5;      // 12300000
var z=123e-5;     // 0.00123
<!DOCTYPE html>
<html>
<body>

<script>
var x1=34.00;
var x2=34;
var y=123e5;
var z=123e-5;
document.write(x1 + "<br>")
document.write(x2 + "<br>")
document.write(y + "<br>")
document.write(z + "<br>")
</script>
</body>
<!-- http://improvejavascript.blogspot.in/ -->
</html>

//output

JavaScript Booleans
Booleans can only have two values: true or false.
var x=true;
var y=false;

TYPE CASTING:
Def: converting from one data type to another data type is called type casting.
In JavaScript variables are automatically type casted(implicitly) based on the literal values that are assigned to it from time to time.
Ex:
Var a=”total”;
Var b=1000;
Var c=a+b;
Document.write(c);//result is “total 1000” 

No comments:

Post a Comment