Javascript’s 9 data types
21 August 2022 (Updated 30 December 2022)
Primitive types (7)
- Strings (
"hello"
,"abracadabra"
, and others), used for text. - Numbers (
-100
,3.14
, and others), used for math calculations. - Booleans (
true
andfalse
), used for logical operations. Has only two values:true
andfalse
. - 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
Thanks for your comment 🙏. Once it's approved, it will appear here.
Leave a comment