What are non-primitive Data Types in JavaScript?
Objects in JavaScript are referred to as non-primitive data types in JavaScript. These data types are derived from JavaScript's primitive data types. These are also referred to as reference data types or derived data types. Variables of non-primitive data types are stored in the heap memory of the system, while primitive data types variables are stored in the stack space of the system.
Types of non-primitive data types in JavaScript:
Data Type | Explanation |
---|---|
Object | It is an instance that allows us to access members in JavaScript. |
Array | It represents a collection of similar elements in JavaScript. |
RegExp | It represents a regular expression in JavaScript. |
- Object: In Javascript, an object is a thing defined using attributes and methods. In Javascript, everything is an object.
Let's see how we can create an object using JavaScript:
- Using a constructor for creating an object:
- Using literal notations for creating an object:
- Array: We can store several elements having similar characteristics under a single name by using an array.
Let's see how we can create an array in JavaScript:
- RegExp: In JavaScript, the RegExp object compares a string to a particular pattern. We can find if a regular expression pattern is present in the string or not using the comparison functions of the RegExp object.
Let's see how we can create a RegExp in JavaScript:
- Using constructor for creating a RegExp:
- Using pattern enclosed b/w forward slashes for creating a RegExp:
Examples of non-primitive data types in JavaScript
1. Object example:
Let's make a student object and print the contents of the object in the console window using a JavaScript program.
Output:
Explanation: We have defined an object named student in the above program and printed the values of the keys firstName and the lastName in the console window.
2. Array example:
Let's declare some random array objects and print their contents in the console window using a JavaScript program.
Output:
Explanation: We have created three different arrays, the first with no arguments (empty array), the second array with a single argument (defining number of elements), and the third array with multiple values (four arguments) in the array.
3. RegExp example:
Let's compare a string "scaler" using the match(regexp) method in JavaScript, and find out if the regexp pattern is present in this string or not.
Output:
Explanation: We have created a RegExp variable regexp with scale pattern/string and i as the modifier that represents case insensitive matching. When we compare this pattern with the string scaler using the match() method, we get the result string that we have printed on the HTML output page.
Learn More
There are seven primitive data types in JavaScript:
- Numbers
- String
- BigInt
- boolean
- undefined
- null
- Symbol
To learn more about the primitive data types, visit Data Types in Javascript.
Conclusion
- Non-primitive data types in JavaScript are derived from the JavaScript language's primitive data types.
- Non-primitive data types in JavaScript are sometimes referred to as reference or derived data types.
- An Object is an instance that allows us to access members in JavaScript.
- An Array represents a collection of similar elements in JavaScript.
- RegExp represents a regular expression in JavaScript.