You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Sentient beings called “humans” use special machines called “computers” to simulate my JavaScript universe.
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.
Counting the Values
Undefined
There is only one value of that type — undefined.
console.log(typeof(undefined)); // "undefined"
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
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
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
There are only two boolean values: true and false:
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:
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:
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.
The text was updated successfully, but these errors were encountered:
04. Counting the Values (Part 1)
The JavaScript Simulation
Sentient beings called “humans” use special machines called “computers” to simulate my JavaScript universe.
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.
Counting the Values
Undefined
There is only one value of that type — undefined.
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.
In fact, if you read a variable that was actually not defined (or before the let declaration), you will get an error:
Null
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:
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
There are only two boolean values: true and false:
Numbers
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:
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:
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:
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
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
The text was updated successfully, but these errors were encountered: