Meeting the Primitive Values Flashcards
Is undefined a value?
Yes. It yields undefined!
what will:
let foo
console.log(foo.bar)
yield?
It will throw a type error. You cannot read properties of undefined - hence all the optional chaining in JS.
What does undefined represent?
A missing value - normally unintentional.
What will:
// no var assignment console.log(foo)
yield?
It will yield a reference error. Variables without assignments are wires to undefined but non instantiated variables are not wires at all, they would just yield errors.
what will:
let foo = null console.log(foo.bar)
yield?
It will throw a type error. You cannot read properties of null - hence all the optional chaining in JS.
How are null and undefined alike?
- There is only one value of each
- both throw errors when trying to read properties from them
- Both represent some absence of ‘value’
What will:
typeof null
yield?
// Object
This is a flipping lie. Due to a bug in JS it pretends to be an object. Null is a primitive not an object. This is just a historical accident we have to live with.
What is the main conventional difference between null and undefined?
Null is for explicitly or manually set empty values and undefined is for implicitly or programmatically set empty values.
List all the primitive values:
Boolean Symbol String Number Bigint Undefined Null
In our mental model, what does a primitive mean?
It means we can’t touch it and it exists outside our code. Our variable merely connect us to those flouting other-worldly values.
I.e. there are only two booleans. False and True. All variables connect to those same two values.
Are numbers infinite in JS?
Nope! Simply run:
Number.MAX_VALUE
to get the max value possible in JS.
Note! BigInt is not a Number (it is its own primitive). This is a different question with that primitive.
What will:
console.log(0.1 + 0.2 === 0.3);
yield and why?
// false
console.log(0.1 + 0.2 === 0.30000000000000004);
This highlights a common issue across various programming languages.
It is called Floating Point Math.
What do you call math where you have to figure out the decimal? i.e. console.log(0.1 + 0.2 === 0.3);
Floating point math.
Does Javascript implement the same math as we use in real life?
No. Javascript implements floating point math which is specifically for computers. Don’t worry too much about the subtleties - it works well enough in practice.
Basically its like a scanner - it approximates the color based on a finite range of options it has available to it. It won’t be the exact same red. JS does the same thing - it approximates numbers based on the 18 quintillion numbers it has available to it. I.e. JS has limited precision.
Basically when we write 0.1 or 0.2, we don’t get exactly 0.1 and 0.2. We get the closest available numbers in JavaScript. They are almost exactly the same, but there might be a tiny difference. These tiny differences add up, which is why 0.1 + 0.2 doesn’t give us exactly the same number as writing 0.3.
How many numbers does JS have?
18 quintillion.