Javascript Types
As covered in Typescript: UI.dev
All languages use types to interpret data in memory and operate on data.
The types in typescript are the same as those in JS, to start with TS we need to understand JS types.
The typeof
operator is helpful in comparing types - it will give you a string representation of the type of a value.
There are six primitive data types in JS - a single specific type of value. They are immutable. We cannot directly modify the value, we can only assign different values to a variable.
The six types are:
Boolean a value that is either true of false. Used for conditionals and logical operators. In JS some values are ‘falsey’. If we cast the value to a boolean via the !!
operation or Boolean(value)
that becomes false. Every other value of the type would be true.
Number in JS numbers are stored as floats, which means you use the same type for integers and decimals. JS also has values for infinity, negative infinity, and NaN (not a number). NaN represents imaginary numbers. Note that if you compare NaN with itself in equality operations it returns false, hence the Number.isNan(value)
method to determine if something is NaN. 0 and NaN are falsey.
BigInt is relatively new, introduced in ES2019. It allows representation of very big integers. We create with a little n at the end of a number literal note that 1234 === 1234n
will return false, and 0n
is falsey.
String stores text data, you can create string literals with double, single, or backtick quotes. Empty strings are falsey.
Symbols are unique and immutable. Any symbol you create will never equal another symbol you create. To create a symbol you pass in a description to help you remember what it is, like let symbol = Symbol("apple")
, but even if you create another with the same description, it will not be equal.
Undefined represents any uninitialized values, including properties of objects that don’t have a value. Any undefined value will be falsey.
Null is an extra type used to represent the absence of a value. There’s a bug where if you use typeof to check the type of a null variable, it will return ‘object’. So you just have to compare with the null literal using the equality operator.
Many bugs in JS occur by trying to access properties on a null or undefined value.
JS also has structural types.
Object The basic structural type - can have any number of properties, all with their own values. We typically use strings to index those properties, but you can use symbols or numbers too. A value can be any type.
Objects are compared by reference, not value, unlike primitive types. So even objects with identical shapes, will compare as false using native equality comparison.
Array Technically the same time as object, but in practice these are very different. They use numbers to index, and so the data is ordered, we can retrieve sequentially. typeof
will return object, we can use Array.isArray(myArray)
to check if an object is an array.
Functions are defined blocks of code that can be invoked elsewhere in the programme. They can take parameters and return a value. Functions can be treated as values, so we can pass them to other functions, return them, and do what we like with them. They are their own type.
Classes are created with the class
keyword, and the use of a constructor
function that takes parameters and uses them to construct a class instance (an object). If you look at the type of the Class
itself, you’ll see it’s a function, representing the construtor of that class.
Then we can call the class like this let motorbike = new Vehicle(2, "black")
. The type of the result is object, and you can use motorbike instanceof Vehicle
to see if an object is an instance of a particular class.