JS: types

cheat sheet

✍️ A type defines how values in memory are interpreted.
✍️ Primitive types: Number, String, Boolean, Undefined, Null. The value itself is stored in the variable.
✍️ Reference types: Object, Array, Function. The variable stores a reference.

let a = 5;
let b = a;
b = 8;
console.log(a, b);
// 5, 8

let x = [5];
let y = x;
y[0] = 8;
console.log(x, y);
// [8], [8]

Lecture

Computer memory consists of zeros and ones. To perform calculations, draw images, and send messages, we must assign meaning to these bits. A bit is 0 or 1. Eight bits make a byte. Groups of bytes can store characters, numbers, and true/false values. A type tells us what meaning we assign to a specific set of bits.

In JavaScript there are primitive types: Number, String, Boolean, Undefined, and Null. When we work with them, we can think of the value as being in the variable itself, and operation results are predictable. Read a = b as: "take the value from b and put its copy into a."

There are also reference types: Object, Array, and Function. In this case, a variable stores a reference to an object somewhere in memory. a = b means: "now a references the same object as b," so changes through b affect what a points to.

values and references

#education #devJS #devTopic