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

[Just JavaScript] 02. The JavaScript Universe

Open allenGKC opened this issue 4 years ago • 1 comments

02. The JavaScript Universe

What is a value? A value is a thing in the JavaScript universe.

Code and Values

To distinguish values from everything else in Dam's JavaScript program, he shows us a picture. He is standing on a small asteroid — it is the code of his program.

image

In the JavaScript universe, values float in space.

image

There are two kinds of value

Primitive Values

Primitive Values are numbers and strings, among other things.

console.log(2);
console.log("hello");
console.log(undefined);

All primitive values have something in common. There’s nothing I can do in my code that would affect them.

Objects and Functions

Objects and Functions are also values, but they are not primitive.

console.log({});
console.log([]);
console.log(x => x * 2);

Objects and functions are special because I can manipulate them from my code.

Expressions

Expressions are questions that JavaScript can answer. JavaScript answers expressions in the only way it knows how — with values.

console.log(2 + 2); // 4

image

We ask JavaScript 2 + 2, and it answers with 4. Expressions always result in a single value.

Checking a Type

If we want to check a value’s type, we can ask it with the typeof operator. JavaScript will answer our question with one of the predetermined string values, such as "number", "string", or "object".

image

Types of Values

Primitive Values

  • Undefined (undefined), used for unintentionally missing values.
  • Null (null), used for intentionally missing values.
  • Booleans (true and false), used for logical operations.
  • Numbers (-100, 3.14, and others), used for math calculations.
  • Strings ("hello", "abracadabra", and others), used for text.
  • Symbols (uncommon), used to hide implementation details.
  • BigInts (uncommon and new), used for math on big numbers.

Objects and Functions

  • Objects ({} and others), used to group related data and code.
  • Functions (x => x * 2 and others), used to refer to code.

In JavaScript, there are no other fundamental value types other than the ones we have just enumerated.

Recap

  • There are values, and then there’s everything else. We can think of values as different things “floating” in our JavaScript universe. They don’t exist inside our code, but we can refer to them from our code.
  • There are two categories of values: there are Primitive Values, and then there are Objects and Functions. In total, there are nine separate types. Each type serves a specific purpose, but some are rarely used.
  • Some values are lonely. For example, null is the only value of the Null type, and undefined is the only value of the Undefined type. As we will learn later, these two lonely values are quite the troublemakers!
  • We can ask questions with expressions. JavaScript will answer to us with values. For example, the 2 + 2 expression is answered with 4.
  • We can inspect the type of something by wrapping it in a typeof expression. For example, typeof(4) is the string value "number".

allenGKC avatar Jul 03 '20 07:07 allenGKC