All of the data types mentioned in the above list have specific constraints. These constraints include the type of value they can store inside their variable, the maximum limit of that value and the maximum size of memory they can consume. To demonstrate the working of some of the following types, look at the following examples below:
To create a string, simple create a variable and set it equal to a string enclosed inside the double quotation marks like:
Display it on the terminal using the console log function:
And the terminal will show the following:
The string has been printed on the terminal, The constraint of defining a string can be the encapsulation with a double quotation mark.
To create a variable of the numbers data type, simply set its value equal to an integer or a floating point like so:
num2 = 20.33;
Executing the following gives the following output on the terminal:
The constraint for creating a number is that it cannot contain any other value than numeric characters and a decimal point.
Undefined is a data type not found in many programming languages. This data type simply defines the variable’s memory as assigned, but no value is placed inside that memory. To demonstrate this, use:
One variable is set to the keyword undefined, and the other is simply created and not given any value. Upon execution of the code, the terminal shows:
Both of the variables returned undefined on the terminal.
4: Booleans & null
Booleans can be defined by creating a variable and setting them equal to the keyword true or false, and null variables can be defined by setting the value equal to the key null. Use the following lines to demonstrate both of these data types:
var y = null;
Upon running the above lines of code, the terminal displays:
Let’s go over the working of the non-primitive data types one by one
To create an object, there are two different ways, one includes the use of the “new Object()” constructor and the other is known as the literal notation. For the new Object() constructor, take the following lines:
obj1.stringVal = "String inside the object";
obj1.intVal = 14;
In the above lines:
- The variable obj1 has been created using the new Object() constructor
- Obj1 has been given 2 values stringVal and intVal, stringVal is a string data type value while the intVal is a number data type value.
- The console log function is used to display the output on the terminal
Executing the above code provides the following result on the terminal:
The variable obj was displayed on the terminal in the object notation. The other way of creating an object variable is by using literal notation. To demonstrate this, use the following lines:
stringVal: "String inside the object",
As it is clear from the code snippet above, to use the literal notation for object definition, simply use the curly brackets as the value of the variable and inside the curly brackets, pass the key-value pairs. Running the above lines of code provides the following output:
The output is the object’s key-value pairs in literal notation
After that, pass this array variable “array1” to the console log function as:
And the result on the terminal will be:
The array1 variable with almost all primitive data types was printed on the terminal