Latest posts

10/recent/ticker-posts

Data Types in JavaScript | How to use Data Tyeps in HTML JavaScript

 Data Types in JavaScript


Generally we know that, to declare anything in variables we need to create a variable first.
But at the time of declaration, we need to provide something before the variable name at the time of declaration.



 [____] variable_name = value;
 or
 [____] variable_name;



[ ____ ] This blank space is reserved for the Data Types !
At the time of declaration of an variable, we just need to Provide Data Type before the variable name.

Example,
 int a = 10;
 or
 int a;

So, Basically the Data Type is a keyword which tells the interpreter or compiler, which type of data we are stored in given variable or which type os Data we want to store in given variable.






Following are the types of Data Types :

There are 8 types of Data Types in JavaScript , But in between as beginner level we are going to see only the mostly used Data Types :

1) Integer / Numbers

Integers are the data types which contains only decimal numbers.
It means when we are declare any variable with integer Data Taype, we can able to store only the decimal numbers in it.
for example,
 int a = 10;
 or
 int a; // variable a can stores only the integers or numbers
 

2) float

Float Data Type is used to store the floating point numbers which contains upto 17 significant digits.
An variable which has float data type, that's store the floating point numbers only.
This type of variable are mostly get in used, when we want to do the calculations like, division, madulation, etc.

Example,
 float a = 10.5;
 or
 float a;
 or
 float b = 10;
 
Here we have declared two variables a & b
Variable a contains value 10.5 and variable b contain value 10
So, the output of a = 10.5 and b = 10.00

But we are declared variable b = 10;
then why its value is becomes 10.00 , because we are declared variable b with data type float.
It means, when we are declared any variable which contains data without floating point, but it's Data Type is float, then it automatically convert it into a floating pinting number and adds double zeros after the floating point by default.



3) String

String data type is used to store the string valus in a variable.
It means it stores Names, Sentences, and single character in a variable.

Example,
 String str = 'Rohan';
 or
 String r = "Hello World !";
 or
 String a;
So, here we have created 3 variables 'str' , 'r' , and  'a'.
Variables str contanis a string 'Rohan'
Variable r contains another string "Hello Wrold !"
and Variable a are does not contains anything, we can able to use it anywhere to store any string in program.

To store the data in variables which has String Data Type, we just need to use single quotes (' ') or Double quotes (" ") around the Values.

When we are declared any variable with Data Type String and we forget to gave the quotes around the values, then the compiler will shows the error message.

4) Boolean

Boolean Data Type is used to Store the data in the form of True or False.
This types of variables are mostly get in the use if we want to store the result of any statement as True or False.

Basically, it returns the result of any comparision or any statement or any calculation as Ture or False.

Exmaple,
 a = (10 > 7);
 print(a); // The variable a contains the boolean value, Which is 
True.
 // So the result of print statement is Ture.
 
 // JavaScript Program

 <!DOCTYPE html>
 <head>
     <title>Data Types</title>
 </head>
 <body>
     <script>
         var a = (10 > 7);
         document.write("Variable A Contains : "+a)
     </script>
 </body>
 </html>
 



So, This are the mostly used Data Types in JavaScript.

But in JavaScript we does not need to Declare any variable with Data Types.
It means we does not need to write the Data Type before the variable name in JavaScript.

Then How to declare the Variables in JavaScript ?

Click on the Following button to see, How to Declare the Variables in JavaScript...




Program which shows the use of All those Data Types in JavaScript




Also Visit


Post a Comment

0 Comments