sajad torkamani

Primitive types (7)

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

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