When it comes to programming, Data types are of supreme importance. And this is valid for JavaScript too. As you might know, we use variables to store data. Well, the different types of data that JavaScript variables can store are collectively termed JavaScript variables.
In this JavaScript tutorial, we will learn about JavaScript types with the help of some really simple examples. So, just read till the end to understand the concept of data type effectively.
What is a Data Type?
The data type in programming is a characteristic used to differentiate data. In simple words, data type defines the type of specific data. For instance, data can be a number or string, or boolean.
Different Data Types in JavaScript
In Javascript, the data types can primarily be categorized into two classes, namely primitive and non-primitive. Let’s understand them one by one.
1. Primitive Data Types
The primitive data type is the predefined data that is only of a single type. In general, primitive data types are defined by the programming language itself and there’s no way a programmer can alter it.
Primitive data types in JavaScript is of 5 types as described in the table below:
Type | Description |
Number | A number can be an integer or a floating point number. Eg. 28, 2021, 19.94. |
String | A string is a combination of characters. Eg. “John”, “Hello”. |
Boolean | A boolean is a value that can be either true or false. |
Null | It represents a null value, which is no value at all. Eg. let x = null; |
Undefined | Undefined value is assigned to a variable whose value isn’t defined during the declaration. Eg. var x; |
2. Non-Primitive Data Types
Non-primitive data types are the ones that are defined by a programmer. Usually, the programmer leverages the primitive data types to define a non-primitive data type.
Non-primitive Data Types in JavScript are of two types that are described in the table as follows:
Type | Description |
Array | An array is a group of values. Eg. const fruits = [“Apple”, “Banana”, “Orange”]; |
Object | An Object is a collection of name-value pairs. Eg. const employee = {firstName = “David”, lastName = “Smith”, designation=“software engineer”}; |
Takeaway
Learning about data types is essential if you want to work with JavaScript or any other programming language. Data type makes it possible for the compiler or interpreter to know how the program is intended to use a particular data.
In JavaScript, the data types are primarily of two types, namely primitive and non-primitive. While primitive data types are available by default and a programmer cannot alter them, the non-primitive data types allow programmers to declare them as per their requirements.
Hi! I am Pankaj, a full-time content specialist and a part-time programmer and marketer. I love to explore new places and also new ideas. I am an inquisitive person with a passion to learn new skills through every possible opportunity that comes in the way.