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
In the previous module, we’ve looked at Undefined, Null, Booleans, and Numbers. We will now continue counting values — starting with BigInts.
BigInts
BigInts were only recently added to JavaScript, so you won’t see them used widely yet. If you use an older browser, they won’t work. Regular numbers can’t represent large integers with precision, so BigInts fill that gap (literally):
let alot = 9007199254740991n; // Notice n at the end
console.log(alot + 1n); // 9007199254740992n
console.log(alot + 2n); // 9007199254740993n
console.log(alot + 3n); // 9007199254740994n
console.log(alot + 4n); // 9007199254740995n
console.log(alot + 5n); // 9007199254740996n
This is great for financial calculations where precision is especially important. Keep in mind that nothing is free. Operations with truly huge numbers may take time and resources.
In our JavaScript universe, there is an infinite number of BigInts — one for each integer in math.
Strings
Strings represent text in JavaScript. There are three ways to write strings (single quotes, double quotes, and backticks), but the result is the same:
What makes objects different is that we can create more of them. Every time we use the {} object literal, we create a brand new object value:
let shrek = {};
let donkey = {};
Do Objects Disappear?
Do objects ever disappear, or do they hang around forever?
let junk = {};
junk = null; // Doesn't necessarily destroy an object
Although we can’t destroy an object, it might eventually “disappear” if there is no way to reach it by following the wires from our code.
Functions
Functions Are Values
To understand functions, we will compare them to numbers and objects.
First, consider this for loop that runs console.log(2) seven times:
for (let i = 0; i < 7; i++) {
console.log(2);
}
How many different values does it pass to console.log? The answer is one value.
Here is another for loop that runs console.log({}) seven times:
for (let i = 0; i < 7; i++) {
console.log({});
}
How many different values does it pass to console.log now? The code above creates and logs seven completely distinct object values.
Let’s have a look at functions:
for (let i = 0; i < 7; i++) {
console.log(function() {});
}
How many different values does this code pass to console.log?The answer is seven.Every time we execute a line of code that contains a function expression, a brand new function value appears in our universe.
Calling a Function
let countDwarves = function() { return 7; };
let dwarves = countDwarves;
console.log(dwarves);
You will see the function itself instead of the number 7 there.
As a result, both countDwarves and dwarves point at the same value, which happens to be a function. See, functions are values. We can point variables to them, just like we can do with numbers or objects.
We want to call a function, we can do that too:
let countDwarves = function() { return 7; };
let dwarves = countDwarves(); // () is a function call
console.log(dwarves);
Adding () changes the meaning of our code:
let dwarves = countDwarves means “Point dwarves towards the value that countDwarves is pointing to.”
let dwarves = countDwarves() means “Point dwarves towards the value returned by the function that countDwarves is pointing to.”
Recap
Undefined: Only one value, undefined.
Null: Only one value, null.
Booleans: Two values: true and false.
Numbers: One value for each floating point math number.
BigInts: One value for every conceivable integer.
Strings: One value for every conceivable string.
Symbols: We skipped Symbols for now, but we’ll get to them someday!
Objects: One value for every object literal we execute.
Function: One value for every function expression we execute.
The text was updated successfully, but these errors were encountered:
05. Counting the Values (Part 2)
In the previous module, we’ve looked at Undefined, Null, Booleans, and Numbers. We will now continue counting values — starting with BigInts.
BigInts
BigInts were only recently added to JavaScript, so you won’t see them used widely yet. If you use an older browser, they won’t work. Regular numbers can’t represent large integers with precision, so BigInts fill that gap (literally):
This is great for financial calculations where precision is especially important. Keep in mind that nothing is free. Operations with truly huge numbers may take time and resources.
In our JavaScript universe, there is an infinite number of BigInts — one for each integer in math.
Strings
Strings represent text in JavaScript. There are three ways to write strings (single quotes, double quotes, and backticks), but the result is the same:
Strings Aren’t Objects
All strings have a few built-in properties.
This doesn’t mean that strings are objects! String properties are special and don’t behave the way object properties do.
A Value for Every Conceivable String
In our universe, there is a distinct value for every conceivable string.
All conceivable string values already exist from the beginning — one value for every distinct string.
Symbols
Objects
Objects includes arrays, dates, RegExps, and other non-primitive values:
Making Our Own Objects
What makes objects different is that we can create more of them. Every time we use the {} object literal, we create a brand new object value:
Do Objects Disappear?
Do objects ever disappear, or do they hang around forever?
Although we can’t destroy an object, it might eventually “disappear” if there is no way to reach it by following the wires from our code.
Functions
Functions Are Values
To understand functions, we will compare them to numbers and objects.
First, consider this for loop that runs console.log(2) seven times:
How many different values does it pass to console.log? The answer is one value.
Here is another for loop that runs console.log({}) seven times:
How many different values does it pass to console.log now? The code above creates and logs seven completely distinct object values.
Let’s have a look at functions:
How many different values does this code pass to console.log?The answer is seven.Every time we execute a line of code that contains a function expression, a brand new function value appears in our universe.
Calling a Function
You will see the function itself instead of the number 7 there.
As a result, both countDwarves and dwarves point at the same value, which happens to be a function. See, functions are values. We can point variables to them, just like we can do with numbers or objects.
We want to call a function, we can do that too:
Adding () changes the meaning of our code:
Recap
The text was updated successfully, but these errors were encountered: