Meeting the Primitive Values Flashcards

1
Q

Is undefined a value?

A

Yes. It yields undefined!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

what will:

let foo
console.log(foo.bar)

yield?

A

It will throw a type error. You cannot read properties of undefined - hence all the optional chaining in JS.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What does undefined represent?

A

A missing value - normally unintentional.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What will:

// no var assignment
console.log(foo)

yield?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

what will:

let foo = null
console.log(foo.bar)

yield?

A

It will throw a type error. You cannot read properties of null - hence all the optional chaining in JS.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How are null and undefined alike?

A
  • There is only one value of each
  • both throw errors when trying to read properties from them
  • Both represent some absence of ‘value’
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What will:

typeof null

yield?

A

// 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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the main conventional difference between null and undefined?

A

Null is for explicitly or manually set empty values and undefined is for implicitly or programmatically set empty values.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

List all the primitive values:

A
Boolean
Symbol
String
Number
Bigint
Undefined
Null
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

In our mental model, what does a primitive mean?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Are numbers infinite in JS?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What will:

console.log(0.1 + 0.2 === 0.3);

yield and why?

A

// false

console.log(0.1 + 0.2 === 0.30000000000000004);

This highlights a common issue across various programming languages.

It is called Floating Point Math.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What do you call math where you have to figure out the decimal? i.e. console.log(0.1 + 0.2 === 0.3);

A

Floating point math.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Does Javascript implement the same math as we use in real life?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How many numbers does JS have?

A

18 quintillion.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What special numbers does floating point math include?

A

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
17
Q

Is NaN a number?

A

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’.

18
Q

What are BigInts?

A

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

19
Q

Are strings objects?

A

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.

20
Q

How does our mental model think about strings?

A

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.

21
Q

What are symbols used for?

A

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.

22
Q

Can all numbers be perfectly represented in JS?

A

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.

23
Q

Where do we have the most precision with floating point math in JS?

A

We have more precision closer to 0 and less precision further away from it.