#javascript
### Sam JavaScript journey
- I've used JS a ton, but never really dove deep, or tried to understand some of the weirder bits
- This has bitten me a number of times, so I've been trying to build better mental models for how it do things
- Here are the things I was surprised by (some of these are embarrassingly basic. Or things I kinda knew but wasn't solid on)
---
### 7 primitives, 9 total types
- Primitives: null, undefined, number, string, boolean, BigInt, symbol
- Other: objects, functions
---
### Values exist independently of code, & primitives specifically are immutable
- JS does some things to make it look like certain types are objects (e.g. string methods), but it's a lie
- You can't have two string values with the same content - it's actually the same value.
```javascript
const foo = "hi"
const bar = "hi"
Object.is(foo, bar) // true, they're the same value
```
---
### Variables themselves merely point to values, and aren't values themselves
- Can't pass variables themselves, they'll always resolve to values
```javascript
const foo = "hi"
const bar = foo
console.log(bar) // "hi", the expression resolves to a value
// No way to pass variable itself
```
---
### JS is historically fucked
- `typeof` claims `null` is an `object`, but it absolutely is not. Historical bug that too much depends on to fix
- Loose equality is a fucking mess
```javascript
console.log(typeof null) // object - fucking lie
// Not even gonna get into loose equality
```
---
### Floating point numbers
- Have never really looked into these tbh
- It's a whole-ass rabbit hole
- There is a finite set of `number` values in JS (some quintillions, but still finite)
- More precise the closer you get to zero
```javascript
console.log(0.1 + 0.2 === 0.3) // false
```
---
### Objects aren't like primitives - they're create-able and mutable
- Every `{}` in an expression creates a new object
```javascript
const foo = {} === {}
console.log(foo) // false
// It's actually creating two different object values
```
---
### There are 3 kinds of equality in JS, not 2
- Loose equality with `==`, strict equality with `===`, and same-value equality with `Object.is()`
- Loose is fucked
```javascript
console.log(0 == false) // true
console.log(0 === false) // false
console.log(Object.is(0, false)) // false
```
---
### Nested objects don't exist
- You can have an object property reference another object, but that object exists independently.
---
### `const` just means you can't point the variable somewhere else
- If it's pointing to an object, you can still mutate that object
```javascript
const foo = {bar: true}
foo.bar = false
console.log(foo.bar) // false
```
---
### Prototypes are basically a backup object
- When trying to read a property on an object, it'll check on that object first, then move to the prototype and check there, recursively.
- Kinda knew this, kinda didn't
---
### The `new` keyword returns an instance of an object, using a constructor and adding a prototype
- Still not super solid on why classes are structured the way they are. Need to dig deeper there.
---
### Three cheers for looking stupid and doing foundational shit 🙂
![[frolicking-meme.webp]]