5 JavaScript core concepts you should know (objects)
Tags: javascript, beginners, webdev
Published: 2019-12-24 22:39:40

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](https://javascriptissexy.com/javascript-objects-in-detail/): 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](https://www.youtube.com/watch?v=9ooYYRLdg_g): 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.

```javascript
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](https://www.codeproject.com/Tips/1130742/Is-Everything-in-JavaScript-an-Object): 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.

```javascript

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:
  1. are stored and copied by value
  2. are immutable
  3. don't have methods assigned to them


4) [Object attributes](https://www.javascripttutorial.net/javascript-objects/): Tell the JavaScript properties how to behave.  They say the object's property:
  1. Value: value
  2. Configurable: can be deleted/changed
  3. Enumerable: can be accessed in a loop
  4. Writable: can be changed

5) [Wrapper object](https://link.medium.com/r8c2tEKTG2): 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.