Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Just JavaScript] 04. Counting the Values (Part 1) #4

Open
allenGKC opened this issue Jul 6, 2020 · 0 comments
Open

[Just JavaScript] 04. Counting the Values (Part 1) #4

allenGKC opened this issue Jul 6, 2020 · 0 comments

Comments

@allenGKC
Copy link
Owner

allenGKC commented Jul 6, 2020

04. Counting the Values (Part 1)

The JavaScript Simulation

Sentient beings called “humans” use special machines called “computers” to simulate my JavaScript universe.

1f8d1b19cd13abc2bd6924c5eb53c42b.gif

This means there are two ways to study it.

Studying From the Outside

You might focus on how a simulation of my world — a JavaScript engine — “really” works.

This approach puts our mental focus on the physical world of people and computers.

Studying From the Inside

We will learn about the JavaScript world for what it is — without thinking about how it’s implemented. This is similar to how physicists can talk about the properties of stars without answering the question of whether the physical world is real. It doesn’t matter! We can still describe it on its own terms.

The foundation of our mental model is that our world is full of values.

5c393b0d926adb80a41a0cb0dbc651b2.gif

Counting the Values

74cf9cbc7de06918d3bd4876ba2ef01d.png

Undefined

There is only one value of that type — undefined.

console.log(typeof(undefined)); // "undefined"

image

Oh, well. Luckily, there is only one undefined in the entire JavaScript universe. You might wonder: why does it exist at all? In JavaScript, it represents the concept of an unintentionally missing value.

let bandersnatch;
console.log(bandersnatch); // undefined

image

In fact, if you read a variable that was actually not defined (or before the let declaration), you will get an error:

console.log(jabberwocky); // ReferenceError!
let jabberwocky;

Null

image

Similarly to undefined, null is the only value of its own type. However, null is also a liar. Due to a bug in JavaScript, it pretends to be an object:

console.log(typeof(null)); // "object" (a lie!)

You might think this means null is an object. Don’t fall into this trap! It is a primitive value, and it doesn’t behave in any way like an object. Unfortunately, typeof(null) is a historical accident that we’ll have to live with forever.

Null is used for intentionally missing values. Why have both null and undefined? This could help you distinguish a coding mistake (which might result in undefined) from valid missing data (which you might express as null).

Booleans

image

There are only two boolean values: true and false:

console.log(typeof(true)); // "boolean"
console.log(typeof(false)); // "boolean"

Numbers

image

console.log(typeof(28)); // "number"
console.log(typeof(3.14)); // "number"
console.log(typeof(-140)); // "number"

A Math for Computers

JavaScript numbers don’t behave exactly the same way as regular mathematical numbers do. Here is a snippet that demonstrates it:

console.log(0.1 + 0.2 === 0.3); // false
console.log(0.1 + 0.2 === 0.30000000000000004); // true

This behavior is common in different programming languages. It even has a name: floating point math.

We can imagine all of the JavaScript numbers on an axis. The closer we are to 0, the more precision numbers have, and the closer they “sit” to each other:

a20c1f9ff949975db3a16239487f87ed.gif

As we move from 0 in either direction, we start losing precision. At some point, even two closest JavaScript numbers stay further apart than by 1:

console.log(Number.MAX_SAFE_INTEGER);     // 9007199254740991
console.log(Number.MAX_SAFE_INTEGER + 1); // 9007199254740992
console.log(Number.MAX_SAFE_INTEGER + 2); // 9007199254740992
console.log(Number.MAX_SAFE_INTEGER + 3); // 9007199254740994
console.log(Number.MAX_SAFE_INTEGER + 4); // 9007199254740996
console.log(Number.MAX_SAFE_INTEGER + 5); // 9007199254740996

Any whole numbers between Number.MIN_SAFE_INTEGER and Number.MAX_SAFE_INTEGER are exact. This is why 10 + 20 === 30.

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

Special Numbers

let scale = 0;
let a = 1 / scale; // Infinity
let b = 0 / scale; // NaN
let c = -a; // -Infinity
let d = 1 / c; // -0
console.log(typeof(NaN)); // "number"

Out of these special numbers, NaN is particularly interesting. NaN, which is the result of 0 / 0 and some other invalid math, stands for “not a number”.

From JavaScript perspective, NaN is a numeric value. It is not null, undefined, a string, or some other type. But in the floating point math, the name for that term is “not a number”. So it is a numeric value. It happens to be called “not a number” because it represents an invalid result.

Recap

  • Not all numbers can be perfectly represented in JavaScript. Their decimal part offers more precision closer to 0, and less precision further away from it. We can say that their decimal point is “floating”.
  • Numbers from invalid math operations like 1 / 0 or 0 / 0 are special. NaN is one of such number. They may appear due to coding mistakes.
  • typeof(NaN) is a number because NaN is a numeric value. It’s called “Not a Number” because it represents the idea of an "invalid" number.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant