What is a non-primitive data type?
Non-primitive data types are JavaScript objects used to bundle multiple items into a single value.
In other words, objects allow us to store multiple items in a JavaScript variable—that accepts only a single element.
Types of JavaScript Objects
JavaScript has four (4) types of objects:
1. Properties object
We use a pair of braces ({...}) to define a JavaScript properties object.
Example
{ month: "November", year: 2022 }
The above properties object contains two properties:
monthyear
We initialized the month property with a string value, and it assigns a number to the year property.
2. Array object
We use a pair of square brackets ([...]) to define a JavaScript array object.
Example
["November", 2022]
The array object in the code above contains two unnamed values.
3. Function object
We use JavaScript function objects to bundle code pieces together and reuse them anytime, anywhere, for an unlimited period.
In other words, functions eliminate the burden of repeatedly writing the same code.
Example
function getTime() {const hourNow = new Date().getHours();const minutesNow = new Date().getMinutes();console.log(`The time is ${hourNow}:${minutesNow}.`);}getTime();
We used the getTime function to bundle three JavaScript statements, thereby making the statements easier to reuse.
4. Regular expression
We use a pair of slashes (/.../) to define a JavaScript regular expression object.
Example
let n = "Welcome to Educative Answers";let searchText = n.search(/Educative Answers/ig);console.log(searchText);
The regular expression in the snippet above contains an Educative Answers pattern and two flags (i and g).
Free Resources
- undefined by undefined