Data types in JavaScript
What you will learn here about javascript data type.
- What is Data type
- Data types in javascript
What is Data type
Data type basically tells what type of data we are going to store in variable. In other programming languages where we have to tell explicitly what type of data we are going to store like integer or float etc. But in javascript we do not need to explicitly mention what type of data we are going to store. Based on value we assign to variable, JavaScript engine (JavaScript engine is a computer program that executes JavaScript code) understands what type of variable it is.
var name=”Jon Dow”;//type of num variable is number
var num=10;//type of bol variable is boolean
var bol=true;
Data types in javascript
There are two types of data types in javascript.
- Primitive data type
- Non Primitive data type
Primitive data type
Primitive data types are data type which can hold or store only one value at time. There are 4 primitive data types and those are
- Number
- String
- boolean
- undefined
1)Number
In javascript, number data type variable can hold integer as well as float value. There is no seperate data type for integer or float.
var num1=10; //this is number
var num2=10.5; // this is also number
2)String
String data type variable can hold String value. In javascript text written inside ” ” (Double quotes) or ‘ ‘ (Single quotes) is considered as String
var name=”John”; //exammple of string
var lastname=’Dow’; //this is also string
3)boolean
boolean data type can have either of two values and those values are true and false.
4)undefined
When we don’t initialize or assign value to variable then default value assigned to variable is undefined and type of variable is undefined.
Non primitive data type
Non primitive data type are data type which can hold or store more than one value at time. Non primitive data types are listed below
- Array
- object
1)Array
Array in javascript is basically collection of hetrogeneous data. Means array can be number array or string array or number and string array etc.
In javascript type of array is object.
2)object
object data type in javascript is similar to structure in C. Object data type can store data in the form of key value pair where key must be unique.
null :
null is value which we can assign to variable. null means nothing. when we assign null value to variable then type of variable is object.