Object Types
Everything that isn't a primitive type is an object type.
Object types are mutable and passed to functions by reference (i.e. modifying the value within a function modifies the original).
There are 3 extremely common built-in object types: Object
, Array
, and Function
.
Objects
The class Object
is the base class of all object types. Objects are associative arrays (a.k.a maps or dictionaries).
Keys can only be strings (or symbols) — non-string keys will be coerced to strings at runtime.
Objects are used extensively throughout JavaScript, both in the standard library and implementation of OOP (classes, inheritance, etc).
Arrays
The Array
class is a subclass of Object
that handles integer keys specially.
We use array literal syntax to declare arrays:
Functions
Functions are objects that can be called. They can assigned to variables and passed as arguments to other functions, just like any other value.
There are two ways to declare functions:
- The
function
keyword - Arrow (
=>
) function expressions
Using objects
Since all object types are subclasses of Object
, even Array
and Function
values are associative arrays. This means we can assign arbitrary keys and values.
However, we generally shouldn't use them like this it's not their intended purpose.
There are some standard library methods and 3rd party frameworks that do this, so it's more a suggestion than a rule.