Just-Javascript-Reading-Notes icon indicating copy to clipboard operation
Just-Javascript-Reading-Notes copied to clipboard

[Just JavaScript] 05. Counting the Values (Part 2)

Open allenGKC opened this issue 4 years ago • 0 comments

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):

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:

console.log(typeof("こんにちは")); // "string"
console.log(typeof('こんにちは')); // "string"
console.log(typeof(`こんにちは`)); // "string"

Strings Aren’t Objects

All strings have a few built-in properties.

let cat = 'Cheshire';
console.log(cat.length); // 8
console.log(cat[0]); // "C"
console.log(cat[1]); // "h"

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

let alohomora = Symbol();
console.log(typeof(alohomora)); // "symbol"

Objects

Objects includes arrays, dates, RegExps, and other non-primitive values:

console.log(typeof({})); // "object"
console.log(typeof([])); // "object"
console.log(typeof(new Date())); // "object"
console.log(typeof(/\d+/)); // "object"
console.log(typeof(Math)); // "object"

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:

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.

allenGKC avatar Jul 06 '20 15:07 allenGKC