Skip to content

Instantly share code, notes, and snippets.

@andresroberto
Last active July 22, 2022 14:48
Show Gist options
  • Save andresroberto/da579585bba7fc1c5f465e5a5b18cd3f to your computer and use it in GitHub Desktop.
Save andresroberto/da579585bba7fc1c5f465e5a5b18cd3f to your computer and use it in GitHub Desktop.
Notes I've taken while reading the ECMAScript Language Types section of the ECMAScript specification.

ECMAScript Language Types

The ECMAScript language types are:

The Undefined Type

The Undefined type has exactly one value, called undefined.

undefined

Any variable that has not been assigned a value has the value undefined.

let foo;

console.log(foo); // undefined

The Null Type

The Null type has exactly one value, called null.

null

The Boolean Type

The Boolean type represents a logical entity having two values, called true and false.

true
false

The String Type

The String type is the set of all ordered sequences of zero or more 16-bit unsigned integer values (“elements”) up to a maximum length of 253 - 1 elements. Each element in the String is treated as a UTF-16 code unit value.

"Hello"

Each element is regarded as occupying a position within the sequence. These positions are indexed with non-negative integers. The first element (if any) is at index 0, the next element (if any) at index 1, and so on.

const greeting = "Hi!";

console.log(greeting[0]); // 'H'
console.log(greeting[1]); // 'i'
console.log(greeting[2]); // '!'

The length of a String is the number of elements (i.e., 16-bit values) within it.

const greeting = "Hi!";

console.log(greeting.length); // 3

The empty String has length zero and therefore contains no elements.

const empty = "";

console.log(empty.length); // 0

The Symbol Type

The Symbol type is the set of all non-String values that may be used as the key of an Object property.

const fooSymbol = Symbol('foo');

const obj = {
  [fooSymbol]: "bar",
};

console.log(obj[fooSymbol]); // "bar"

The Numeric Types

ECMAScript has two built-in numeric types: Number and BigInt.

The Number Type

The Number type includes double-precision 64-bit format IEEE 754-2019 values as specified in the IEEE Standard for Binary Floating-Point Arithmetic, except that the distinct “Not-a-Number” values of the IEEE Standard are represented in ECMAScript as a single special NaN value.

42
1984
12.5
-23
NaN

There are two other special values, called positive Infinity and negative Infinity, produced by the program expressions +Infinity (or simply Infinity) and -Infinity.

Infinity
-Infinity

Note that there is both a positive zero and a negative zero, produced by the program expressions +0 (or simply 0) and -0.

0
-0

The BigInt Type

The BigInt type represents an integer value. The value may be any size and is not limited to a particular bit-width.

42n
BigInt(42)
-42n
BigInt(-42)

The Object Type

An Object is logically a collection of properties.

{
  name: "John Doe",
  age: 42,
  cool: true,
}

Properties are identified using property keys. A property key is either a String value or a Symbol value. All String and Symbol values, including the empty String, are valid as property keys.

const obj = {};

// String as property key (a.k.a. property name)
Object.defineProperty(obj, "foo", { value: "bar" });

// Symbol as property key
Object.defineProperty(obj, Symbol("baz"), { value: true });

// Empty string as property key
Object.defineProperty(obj, "", { value: 42 });

A property is either a data property, or an accessor property.

A data property associates a key value with an ECMAScript language value and a set of Boolean attributes.

const obj = {
  hairColor: "black",
};

Object.defineProperty(obj, "name", {
  value: "John Doe",
  enumerable: true,
  configurable: true,
  writable: true,
});

obj.age = 42;

obj["cool"] = true;

console.log(obj.hairColor); // "black"
console.log(obj.name); // "John Doe"
console.log(obj.age); // 42
console.log(obj.cool); // true

An accessor property associates a key value with one or two accessor functions, and a set of Boolean attributes.

const obj = {
  get greeting() {
    return `Hi, I'm ${this.name}`;
  },
};

Object.defineProperty(obj, "name", {
  set: function(name) {
    this._name = name.trim();
  },
  get: function() {
    return this._name || "Anonymous";
  },
  enumerable: true,
  configurable: true,
});

console.log(obj.name); // "Anonymous"

obj.name = "   John D.   ";

console.log(obj.name); // "John D."

console.log(obj.greeting); // "Hi, I'm John D."

obj.greeting = "¡Hola!";

console.log(obj.greeting); // "Hi, I'm John D."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment