sajad torkamani

Primitive types (7)

  • Strings ("hello""abracadabra", and others), used for text.
  • Numbers (-1003.14, and others), used for math calculations.
  • Booleans (true and false), used for logical operations. Has only two values: true and false.
  • Null (null), used for intentionally missing values. Has only one value: null.
  • Undefined (undefined), used for unintentionally missing values. Has only one value: undefined.
  • Symbols (uncommon), used to perform rituals and hide secrets.
  • BigInts (uncommon and new), used for math on big numbers.

Objects and Functions (2)

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

What about arrays, dates, regular expressions and so on?

Other common data types like arrays, dates, and regular expressions are all objects.

console.log(typeof([])); // "object"
console.log(typeof(new Date())); // "object"
console.log(typeof(/(hello|goodbye)/)); // "object"

Use typeof to check the type of a value

console.log(typeof(2)); // "number"
console.log(typeof("hello")); // "string"
console.log(typeof(undefined)); // "undefined"

Asking for typeof(null) will return object instead of null. This is a bug in JavaScript that’ll probably never be solved because it would break a lot of existing code (see this post).

Tagged: JavaScript