Meeting the Primitive Values Flashcards
(23 cards)
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.
What special numbers does floating point math include?
Nan
Infinity
-Infinity
-0
They exist because you might execute an operation like:
1/0
Here are some examples:
let scale = 0; let a = 1 / scale; // Infinity let b = 0 / scale; // NaN let c = -a; // -Infinity let d = 1 / c; // -0
Is NaN a number?
Yes. It’s not lying, In floating point math NaN is a numeric value which stands for ‘not a number’. It is a numeric value which means ‘invalid result’.
What are BigInts?
BigInts are one of 7 Primitive values in JS. They help us work with massive integers that numbers can’t adequately represent (being finite as they are). BigInts are not finite - but computers need the power to work with big versions of them. Big ints are represented with an ‘n’ at the end of an integer like so:
9007199254740991n
Are strings objects?
Nope - strings are their own primitive. One of the 7. But they do have a few built in properties such as:
‘poop’.toUpperCase()
Strings are immutable so things like
‘poop’[0] = ‘s’
can’t yield ‘soop’. It would just do nothing at all.
How does our mental model think about strings?
In our universe, there is a distinct value for every conceivable string. So there is only ever one ‘poop’ and it exists all the time - out of reach but we can point to it - i.e. as a primitive we can reference it but never alter it.
What are symbols used for?
Symbols serve a similar purpose to door keys: they let you hide away some information inside an object and control which parts of the code can access it.
Can all numbers be perfectly represented in JS?
Nope. Floating point math means we’re restricted in accuracy. BigInts allow us to have any possible size of integer but that doesn’t help with decimals.
Where do we have the most precision with floating point math in JS?
We have more precision closer to 0 and less precision further away from it.