What are actually Data Types?
A data type defines the type, the kind of data that is stored within a declared variable.
It is important to understand different data types, so you are aware of characteristics of each type, because this can have an impact on the code later on.
- Primitive data types
- Non-primitive data types
The main characteristic of primitive data type is the ability to hold or store only one value at a time.
There are five primitive data types:
String Data Type
Strings represent collections of characters and symbols, whether numeric or alphabetical, surrounded by single or double quotes.
This represents the way to store letters, words, and phrases:
let string = "Hello"; let string2 = "This is also a string"; let string3 = "And this is a string number 3"
let stringName = String("Creating a string using an inbuilt JS function");
Numbers Data Type
Numbers data type is the obvious data type. It can be an integer, or a decimal number, even an exponential number.
These variables are used for mathematical operations later in the code, or as counters.
let number = 100; //integer let decimal = 10.5; //decimal number let exp = 10e5; //exponential number
Please keep in mind that numbers are declared without using quotes. If quotes are used, the variable data type will be a string, not a number.
let numberVariable = 100; //a number // however... let otherVariable = "100"; //this is a string, not a number
let newNumber = 1 / 0; // this operation will result in INFINITY // and that will have a number data type
NaN represents an error in computing, an undefined mathematical operation.
let newNumber = "a string" / 5; // here as a result we will get NaN // and it will also have a number data type
Infinity and NaN ensure we always get some result of our operations, and the code will not break.
Boolean Data Type
Boolean is an interesting data type, as it only has two possible results: true and false.
This is a logical data type, used to store yes or no states:
let isSelected = true; let isChecked = false;
Boolean data type does not always need to be declared. It can be a result of an logical expression, which can be useful as a trigger for later code or some function, especially as a part of IF statements.
let a = 5; let b = 2; alert(a > b); // a in fact has a greater value than b, which will return true as a result
Undefined Data Type
Undefined is a special data type, as it can have only one value – undefined.
It is mostly the case when the variable has no declared value.
let a // the "a" variable is only declared here, without a designated value // this means that "a" variable is not defined, and if we use it anywhere in our code // it will result in a value UNDEFINED
Null Data Type
Null is another special data type, as it only refers to one value – null. This is a value that represents nothing, a not existing value.
It is not zero, a number 0, or an empty string ‘ ‘. The difference is that null is a non-value data type:
let a = 0; // this is not a null data type, it is in fact a number, zero let b = ''; // this is a string, empty, but still a string let c = null; // and this is NULL data type, non-existing, unknown, emptiness
Difference between null and undefined is that undefined is the result of not defining a variable. But with null, you have to intentionally declare it. So we use null when we do not wish our variable to have a value at all.
Non-primitive Data Types
Non-primitive data types are complex data types. Unlike primitive data types, which can hold only one value, non-primitive data types represent collections of data. With these collections, we can group multiple data elements (and multiple primitive data types) into one package.
The non-primitive data types are:
There are five basic, or primitive, types of data. The five most basic types of data are Strings, Numbers, Boolean, Undefined, and Null.
Besides primitive data types, there are also more complex, non-primitive data types. These are Objects, Arrays, and Functions.