Disclaimer: this blog post was written on my phone. So please excuse any grammar errors and typos.
JavaScript objects is one of the most important concepts in JavaScripts. As I reviewed objects, I kept track of the following core concepts to understand:
1) Object: The most used data type in JavaScript. Everything in JavaScript (except primitives) are objects. This includes arrays, functions and built-in objects. Even primitive data types, like strings and numbers can be temporarily converted to objects that have assigned methods. An example of this is calling string.toUppercase()
.
Strings are technically primitives. But, in this example, JavaScript temporarily coerces the string to a string object in order to use the .toUppercase()
method.
Think of object as a collection or list of key-value pairs:
1. Values: Also called properties, or methods if they are functions- can be primitives objects themselves
2. Keys: The names used to access an object's properties.These can be strings, numbers or symbols.
It's worth noting that when the key is a number, the property can only be accessed with bracket notation
2) Reference data type: JavaScript objects are also called reference data types. This is because their values are stored by reference. Instead of storing the actual data to memory, JavaScript actually stores a reference to this data. Even if you assign an two different variables, changing the property through one variable, will also change the property in the other variable.
let obj = {
"name": "jasterix",
"location": "NYC"
}
let obj2 = obj
console.log(obj.name) // result: jasterix
obj2.name ="changed"
console.log(obj2.name) // result: changed
console.log(obj.name) // result: changed
This is because both variables point to the same location in memory
3) Primitive data type: Unlike reference types, primitive data types actually store data in memory. If you set one variable equal to another variable, JavaScript copies the data of the first variable into the second.
let varr = 5
let varr2 = varr
varr = 6
console.log(varr) // result: 6
console.log(var2) // result: 5
Unlike the prior example, changing the value of one variable does not affect the value of the other.
There are 6 primitive data types in JavaScript, These are
booleans, number, strings, null, undefined and symbols. These are primitives because they:
- are stored and copied by value
- are immutable
- don't have methods assigned to them
4) Object attributes: Tell the JavaScript properties how to behave. They say the object's property:
- Value: value
- Configurable: can be deleted/changed
- Enumerable: can be accessed in a loop
- Writable: can be changed
5) Wrapper object: How you are able to call the method string.toUppercase()
, even though strings are primitives. A wrapper object lets JavaScript coerce the string to an object through the new
keyword. Wrapper objects are available for numbers, booleans and strings.
Instead of one encompassing resource per blog post, someone recommended providing a resource for each concept. I hope you find this help.
As always, I welcome all comments and criticisms of my explanation of these JavaScript concepts.
Top comments (0)