In the dynamic world of web development, data plays an important role. The JavaScript world is full of different data types that have different uses in the development of software and websites. In this article, we will learn about data types in JavaScript and learn how to use them in JavaScript code.
Table of Contents
Types of data types in JavaScript
In JavaScript, data is divided into two main categories: Primitive data and non-primitive data.
1. Primitive data
Primitive data is data that is stored directly in memory. These data types include numbers, strings, boolean values, and undefined and null values.
Suppose you have a variable named “credit” and the value of this variable is equal to 50 dollars. Here “credit” is a variable with Primitive data. The value of $50 is stored in memory for this variable.
2. Non-Primitive Data
Non-primitive data is a set of primitive data that are stored as references in memory and refer to the data storage location instead of keeping the actual value of that variable. These data usually include arrays, objects, and functions.
Suppose you have a variable named “BooksList” and this reference variable points to a list of books. Here “BooksList” is a variable with Non-Primitive data. This variable points to a location where a list of books is stored, but the values of books are not directly in “BooksList”.
Introduction of JavaScript data types
There are 8 data types in JavaScript language:
- String
- Number
- BigInt
- Boolean
- Undefined
- Null
- Symbol
- Object
All these data, except for Object, are of primitive data type.
In the rest of this article, we will introduce all these data types.
1. String
String is used to store text. In JavaScript, strings are shown between double quotation marks ” ” or ” “.
If you put the text of the string between two backticks or “, you can put the variables or expressions you want inside `{}$` so that their value is dynamically placed in the string.
2. Number
The number indicates integers and floats (decimal and exponential).
+infinity and -infinity values and undefined values (Not a number) are also considered as Number datatype.
3. BigInt
In JavaScript, the BigInt data type is used when the integers are very large and out of the normal range of integer numbers.
To define a BigInt number, just add the letter n to the end of the desired integer. In this way, JavaScript understands that this number should be interpreted as a BigInt.
4. Boolean
In JavaScript, the Boolean data type is used to represent the logical values True and False. These two values are commonly used to control conditions and make logical choices in programming. For example, you can use these values and decide whether to operate based on their “yes” or “no” status.
5. undefined
In JavaScript, if a variable is defined but not assigned a value, the value of this variable is undefined.
6. null
In JavaScript, null means an empty or unknown value. In other words, when a variable is set equal to null, it means that the variable does not yet have a value or its value is unknown.
Note that null is not the same as NULL or Null and must be typed in lowercase to be recognized correctly.
What is the difference between undefined and null?
Understanding the difference between null and undefined in JavaScript may be a bit confusing at first, but these two data types have completely different meanings and uses.
undefined indicates that a variable has been defined but has not been assigned a value. In other words, undefined indicates the state of not defining the value.
On the other hand, null indicates that a variable is defined and assigned a value that is either intentionally set to null or the value is undefined.
7. symbol
In JavaScript, Symbol data types are values that cannot be changed and each Symbol is created uniquely.
Symbol is usually used to create unique properties in objects and avoid conflicting their names in the program.
8. Object
In JavaScript, an object is a complex data type that allows the programmer to store collections of data. In other words, Object allows us to store a set of different information in one place.
The object is very useful when we want to store and manage diverse and related data together.
The method of identifying data types in the code
In JavaScript, the type of operator is used to determine the type of a variable. The type of operator is used in JavaScript to get the data type of a variable. This operator allows the programmer to recognize the data type and perform appropriate operations based on it.