What is a Data Type?
As we all know the computers are machines that process data. Data is stored in variables (as discussed earlier). Variables have some unique characteristics such as:
- Data type
A data type defines the type of information stored in the computer memory (using a variable).
- Positive integers: 1, 2, 3 …
- Alphabetical characters: a, b, c…
- Dates from the calendar: 19-Jan-2019 and so on.
- The variable types are not explicitly defined
- The type of variable can be changed at runtime.
var count=1; //variable holds an integer value
count =hello; //the same variable now holds a string value
var name=’Shagufta’; //variable holds a string value
name=4.5; //variable holds a floating-point number value.
Primitive and Reference Value:
- Primitive values are the simple piece of data that are stored on the stack.
- That means the value is stored directly in the location from where the variable is accessed.
- Reference values are objects that are stored in heap.
- That means the value stored in the variable location as a pointer to a location in memory where the object is stored.
The primitive data types are:
The Special data types are:
The composite (reference) data types are:
String data types:
It represents a sequence of characters enclosed with single quotes (‘) and double quotes (‘’) respectively.
Number data types:
The number of data types deal with digits. It covers both floating-point numbers as well as integers.
Floating-point numbers are like 4.22 and 0.98
Integer numbers are like 25, 125, and 1200
Infinity and NAN:
Infinity is a property of a number.it represents mathematical infinity.
Example: var infinity=1e400*1250;
NaN stands for not a number and it’s a result of a mathematical operation that doesn’t have sense. This popup when we divide 0 by 0.
Example: Var x=0/0;
Boolean data types:
The Boolean type has two values, true and false.
var exist= true;
Undefined data types:
Undefined has only one value Undefined. It means a variable has not been defined, no such variable exists in the current scope. In simple words, nothing is stored in the variable. Undefined is different from that null.
In the above variable name x, no value is assigned to it.
Null data types:
The null type has only one value Null. It means that an object exists and is empty. In simple words, the null values mean no data or this variable doesn’t have any data stored in it.
Object data type:
var objectname=new Object();
Array data type:
The array is an ordered sequence of elements.
If we want multiple variables of the same or different types then remembering names of variables is a problem. The concept of the array allows us to store different data type data information under the same name in a sequence ordered way. It helps in creating many variables. There is no need for remembering their names because they all have the same name but different positions in the array. The counting in an array starts from 0, not from 1, means the first location is 0th location and 12th is 11th.
Example: Var b=new Array (“one”,”two”,”three”,”four”);
In the above example, we have declared an array variable.