The JavaScript Universe Flashcards
What are values in JS?
Values are: booleans, numbers, strings, symbols, functions and objects, null and undefined.
Things like if statements, loops and variable declarations aren’t values.
MDN defines JS objects as follows:
The set of types in the JavaScript language consists of primitive values and objects.
Primitive values (immutable datum represented directly at the lowest level of the language)
Boolean type Null type Undefined type Number type BigInt type String type Symbol type
and
Objects (collections of properties)
Are values in our code?
No, values are not ‘in our code’. They exist in the runtime. Our code interacts with values rather than being comprised of values.
Like the little prince looking up at the night sky, we might refer to those stars but those stars aren’t ours. We might refer to the values but they aren’t in our code.
What does it mean to ‘give it 5 minutes’?
Essentially it’s to think rather than react - give ideas 5 minutes to sink in, give them the time of day before denouncing them.
What are the two different types of values in javascript?
Primitive values and ‘objects and functions’.
What is a primitive value?
Like cold, distant stars - primitives exist outside the code but we’re able to point to them. Examples of primitives are numbers like 2 and strings like “poop”.
Importantly you can’t touch or manipulate stars. They can’t be created by us and are permanent, unchanging and untouchable.
What is a non-primitive value?
Like asteroids in orbit we can access these values and manipulate them. They’re not part of your world (code) but they’re close enough to manipulate.
What are functions?
Functions are really just objects with a few additional features.
List all 9 different types of values in JS.
String, Boolean, Number, Object, Function, Null, Undefined, BigInt, Symbol
What is the common use case for undefined vs null?
Null is used to explicitly set missing value.
Undefined is used for unintentionally missing values.
List all primitive values.
Number, String, Boolean, Null, Undefined, BigInt, Symbol
What are two of the most common non-primitive values?
Objects and Functions.
In JS, is everything an object?
Nope. Primitives might behave like objects ( “hi”.toUpperCase() ) but that’s just an illusion.
The JS engine creates a temporary object for this.
In JS, are functions objects?
Yes.
Functions are just objects with special, built-in methods and properties.
Functions are not just any old objects, they are Function objects, meaning that besides being able to assign properties and methods to them, they also have properties and methods already defined by the built-in Function object.
Functions share many characteristics with objects. But what distinguishes them is the fact that they can be ‘called’.
What is a good mental model for typeof?
A telescope - used to identify what X is floating around our little moon.
What is the correct name for labeling the following:
> 2 + 2
typeof(foo)
Expression.
Expressions are questions that JavaScript can answer. JavaScript answers expressions in the only way it knows how—with values.
Expressions always result in a single value.