- What is a Data Type?
var x = 10;
Please have a look at the below example. Here, we define the x variable and assigned the value 10 to it. And we are logging the data type in the console window using the console.log method. As discussed, the typeof method gives the data type.
Once you run the above program, open the browser console window by pressing the F12 key or Function + F12 key and you will get the following output. As we are assigning a number (i.e. 10) to the variable x, so it is showing the data type as a number.
When you run the above code, you will get the following output in the browser console. You can get the browser console by pressing the F12 or function F12 key.
If you declare a variable without assigning any value to it, then the data type of that variable becomes undefined. Let us understand this with an example. In the below example, we declare the variable x without assigning any value and then we try to figure the data type of the variable x using the typeof function.
Output: When you run the above code, you should get the data type as undefined in the browser console window as shown in the below image.
Don’t confuse undefined with null. Both are two different things altogether. Null represents nothing. That means null is neither a number, a string, or a Boolean. Null means nothing. Sometimes, in your program you want to represent nothing then at that time you need to use null. On the other hand, if you create a variable without assigning any value, then the data type of that variable is undefined.
Let us understand the above two things with an example. In the below example, we declare the variable x without assigning any values and hence it will print undefined. On the other hand, we declare the variable y and assign this variable with the value null and when you print this variable, you will get null.
Output: When you run the above code, you will get the following output in the browser console window.
The primitive data types are:
The Special data types are:
The composite (reference) data types are:
It represents a sequence of characters enclosed with single quotes (‘) and double quotes (‘’) respectively.
Number Data Type:
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;
The Boolean type has two values, true and false.
var exist= true;
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.
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:
Example: 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 the 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, which means the first location is the 0th location and the 12th is the 11th.
Example: var b=new Array (“one”,”two”,”three”,”four”);
In the above example, we have declared an array variable.
About the Author: Pranaya Rout
Pranaya Rout has published more than 3,000 articles in his 11-year career. Pranaya Rout has very good experience with Microsoft Technologies, Including C#, VB, ASP.NET MVC, ASP.NET Web API, EF, EF Core, ADO.NET, LINQ, SQL Server, MYSQL, Oracle, ASP.NET Core, Cloud Computing, Microservices, Design Patterns and still learning new technologies.