Basically data types are used to classify one particular type of value(data), that can be represented and manipulated data in a programming language.
Introduction of datatype
A datatypes is the specific category of information that a variable contain. It classify types of data such as string, boolean or numbers etc. So that, It determines the possible values for that particular type.
The data-type type also governs the kinds of operations that can be performed on a variable. This is important because the specific data-type you use will determine what values you can assign to it and what you can do to it.
Note : Datatypes in javascript means the type of data stored in variable.
Why data-type is used in programming language ?
The conscept of data-types is often difficult for beginning programmers to grasp because in real life you don't often distinguish among different types of information. If someone asks you for your name, your age or the current time, you dont't usually stop to consider that your name is a text string and that your age and current times are numbers.
However, a variables specific data-type is very important in programming because the data-type helps determine how much memory the computer allocates for the data stored in the variable.
Numbers(Numerical Data).
String(Text Data).
Boolean.
Symbol.
Undefined
Null
Array
Function, Object and Properties.
JavaScript classifies data-type into 3-section :
1.) Primitive Datatype : Following are the primitive data-type that JavaScript supports.
2.) Special Datatype : Following are the Special data-type that JavaScript supports.
3.) Non-Primitive Datatype : Following are the non-primitive data-type that JavaScript supports.
1.) Primitive Datatypes :
Datatype's that can be assigned only a single value with no additional properties and methods are called primitive types.
Primitive defines immutable values and was introduced recently by ECMAScript standard, It can be stored in the fixed amount of memory available.
Let's see an example of primitive data-type :
➤ Example : Copying a primitive.
In this example, The original was not changed, we can only change the copy.
<script> var a = 20; // assign `20` to `a` var b = a; //copy the value of `a` to `b` b = 25; //assign `25` to `b` console.log(a) // =>20 </script>
Note : Primitive types are also known as simple types or scalar types.
JavaScript supports 4-types of primitive data types described in table :
Datatype | Description | Example value |
---|---|---|
Number | Represents numeric(Positive or negative numbers with or without decimal places, or number written using exponential notation) values. | 9, 9.5 etc. |
String | A string (or a text string) is a series of characters. | "Hello" “Hello Coder” etc. |
Boolean | A logical value of true or false. | true, false. |
Symbol | (ECMAScript 6) |
2.) Special Datatype :
Special data-type are also known as Primitive types.
Javascript has 2-more literals, null and undefined, that is considered to be objects.
Following are the Special data-type that JavaScript supports, described in table:
Datatype | Description | Example value |
---|---|---|
Undefined | represents undefined value(A variable that has never had a value assigned to it, has not been declared, or does not exits). | Undefined value |
Null | represents numeric values(An empty value). | Null value |
3.) Non-Primitive Datatypes :
Datatype's that Objects can contain multiple values are called non-primitive types. It is same as object in the class. We can define attributes and methods to composite data-types.
A Non-Primitive datatypes can contain other values. Since the contents of a reference type can't fit in the fixed amount of memory available for a variable, the in memory value of a reference type is the reference itself (a memory address).
Array
RegExp
Function, Object and Properties.
For instance :
Let's see an example of non-primitive data-type :
➤ Example : Copying a reference;
The original was also changed, since the reference got copied.
<script> var data = {number: 22 }; //assign the reference of a new object to `data`. var b = data; //copy the reference of the object inside `data` to new variable `b`. b.number = 75; //modify the contents of the object `b` refers to. console.log(data); //=> { number : 37 } </script>
Note : Non-Primitive type are also known as : Reference types or Composite data-type or Complex types or container types.
JavaScript contain following non-primitive data types, described in table:
Datatype | Description |
---|---|
Array | represents group of similar values. |
RegExp | represents regular expression. |
Object | represents instance through which we can access members. |
Comments