DEV Community

kycodee
kycodee

Posted on

Javascript's Complex/Non-Primitive Data Types

There are two main groups of data types in Javascript, primitive and non-primitive. Non-primitive data types, also known as complex data types, are what we'll be discussing in this read. There are two complex data types that we seem to use all of the time when working with code. These are arrays and objects. Complex data types are a place where we can store values and is often composed of multiple primitive values such as numbers, strings, booleans, etc.

Objects

Objects are usually used to store pieces of data about a specific person, place, or thing. When objects are defined, they are defined using key:value pairs. The keys in the object, also often called properties, are like the characteristics of the person or thing. For example, if we were creating an object for a car, we may have a property called 'make'. This property would refer to the make of that specific car. Now on to the second part of the key:value pair, the value. The values in an object are usually of the primitve data type, but can also be non-primitive, such as arrays and objects. In this case of making a key:value pair for a car's make will result in something like a property of 'make' and a value of 'Ford'. It would be written just like this.

const car = {
     make: 'Ford'
}

console.log(car) //logs to the console {make: 'ford'}
Enter fullscreen mode Exit fullscreen mode

Arrays

An array is another important non-primitive data structure that is commonly used when programming. Like objects, they are used to store data. They are different from objects, because they are used to group together similar elements without going into detail about their characteristics using key:value pairs. Arrays are made using brackets and it's elements are seperated by commas. If we were to use the same example as object and make an array about cars, it would most likely be an array of elements that are the makes of cars. For example:

const cars = ['toyota', 'audi', 'dodge', 'honda', 'ford']

console.log(cars) //logs to the console => ['toyota', 'audi', 'dodge', 'honda', 'ford']
Enter fullscreen mode Exit fullscreen mode

In conclusion, complex data types are great ways to store values in Javascript. Learning how to use them will benefit you tremendously since they are needed even on the most basic levels of programming. Once you learn to store values using arrays and objects, you will have no problem moving on to more complex things like functions, especially higher-order functions.

Top comments (0)