文章目錄
BigInts
BigInts were only recently added to JavaScript, so you won’t see them used widely yet. If you use an older browser, they won’t work. Regular numbers can’t represent large integers with precision, so BigInts fill that gap (literally):
|
|
String (primitive value)
Strings Aren’t Objects
Strings Aren’t Objects.All strings have a few built-in properties.
|
|
This doesn’t mean that strings are objects! String properties are special and don’t behave the way object properties do. For example, you can’t assign anything to cat[0]. Strings are primitives, and all primitives are immutable.
Object
|
|
Unlike everything before, objects are not primitive values. This also means that by default, they’re mutable. We can access their properties with .
or []
|
|
Making Our Own Objects
In our mental model, all of the primitive values we’ve discussed — null
, undefined
, booleans, numbers, and strings — have “always existed”. We can’t “make” a new string or a new number, we can only “summon” that value:
|
|
Every time we use the {}
object literal, we create a brand new object value:
|
|
Do Objects Disappear?
You might wonder: do objects ever disappear, or do they hang around forever?
JavaScript is designed in a way that this is not directly observable from our code (although that might change).
You can’t destroy an object that you’ve created:
|
|
JavaScript is a garbage-collected language. Practically, this means that values might eventually disappear from my universe if I can’t reach them by following any wires from my code. But JavaScript doesn’t offer guarantees about when that happens.
In my universe, objects and functions float closest to my code. This reminds me that I can manipulate them and even make more of them.
Functions
Consider this example:
|
|
How many objects did we create? We haven’t talked about scopes yet, so it’s fine if you can’t figure out a diagram for this snippet of code yet. The answer is that we create seven objects — one in each iteration of the loop. Now look at this example:
|
|
How many functions do you see here? Is it one function, or is it seven?
The snippet above contains one function definition in the code, but it creates seven function values! This is why separating these concepts is important.
Every time we execute a line of code that contains a function declaration, a brand new function value appears in our universe.
This might remind you of how whenever we execute code like
let dwarf = {}
, a new object appears.
We create objects, and we create functions.
Recap
- Counting the Values (Part 1)
- Numbers: One value for each floating point math number.
- BigInts: One value for every conceivable integer.
- Strings: One value for every conceivable string.
- Symbols: We skipped Symbols for now, but we’ll get to them someday!
The types below are special because they let us make our own values:
- Objects: One value for every object literal we execute.
- Function: One value for every function definition we execute.
writing 2 or “hello” always “summons” the same number or a string value.
But writing {}
or declaring a function always creates a brand new, different value.
This idea is crucial to understanding equality in JavaScript.