Primitive types vs objects in JavaScript
What is the main difference between primitive types and objects in JavaScript?
First, let’s define what are primitive types.
Primitive types in JavaScript are
- strings
- numbers (Number and BigInt)
- booleans (true or false)
- undefined
- Symbol values
null is a special primitive type. If you run typeof null you’ll get 'object' back, but it’s actually a primitive type.
Everything that is not a primitive type is an object.
Functions are objects, too. We can set properties and method on functions. typeof will return 'function' but the Function constructor derives from the Object constructor.
The big differences between primitive types and objects are
- primitive types are immutable, objects only have an immutable reference, but their value can change over time
- primitive types are passed by value. Objects are passed by reference
- primitive types are copied by value. Objects are copied by reference
- primitive types are compared by value. Objects are compared by reference
If we copy a primitive type in this way:
let name = 'Flavio'
let secondName = name
Now we can change the name variable assigning it a new value, but secondName still holds the old value, because it was copied by value:
name = 'Roger'
secondName //'Flavio'
If we have an object:
let car = {
color: 'yellow'
}
and we copy it to another variable:
let car = {
color: 'yellow'
}
let anotherCar = car
in this case anotherCar points to the same object as car. If you set
car.color = 'blue'
also
anotherCar.color
will be 'blue'.
The same works for passing around objects to functions, and for comparing.
Say we want to compare car to anotherCar:
anotherCar === car //true
This is true because both variables point to exactly the same object.
But if anotherCar was an object with the same properties as car, comparing them would give a false result:
let car = {
color: 'yellow'
}
let anotherCar = {
color: 'yellow'
}
anotherCar === car //false download all my books for free
- javascript handbook
- typescript handbook
- css handbook
- node.js handbook
- astro handbook
- html handbook
- next.js pages router handbook
- alpine.js handbook
- htmx handbook
- react handbook
- sql handbook
- git cheat sheet
- laravel handbook
- express handbook
- swift handbook
- go handbook
- php handbook
- python handbook
- cli handbook
- c handbook
subscribe to my newsletter to get them
Terms: by subscribing to the newsletter you agree the following terms and conditions and privacy policy. The aim of the newsletter is to keep you up to date about new tutorials, new book releases or courses organized by Flavio. If you wish to unsubscribe from the newsletter, you can click the unsubscribe link that's present at the bottom of each email, anytime. I will not communicate/spread/publish or otherwise give away your address. Your email address is the only personal information collected, and it's only collected for the primary purpose of keeping you informed through the newsletter. It's stored in a secure server based in the EU. You can contact Flavio by emailing flavio@flaviocopes.com. These terms and conditions are governed by the laws in force in Italy and you unconditionally submit to the jurisdiction of the courts of Italy.