Introduction¶
In JavaScript, data is divided into two categories:
- primitive types (simple) - they are used to save simple data
- reference types - are used to save complex objects
All non-simple variables are a reference type, eg objects or arrays.
Primitive types¶
There are the following primitive types in JavaScript:
string
- represents a stringnumber
- represents a numeric value. In JS,number
represents both integers and floating point numbersboolean
- represents a boolean value, i.e. true (true
) or false (false
)bigint
- capable of storing an integer with a maximum value greater than the maximum value ofnumber
undefined
- special representation of the value undeclared , not existing in memorysymbol
- unique identifier of the object
NOTE: in a JS string we can create both using the characters
'
and"
.
Reference types¶
There are 3 reference types in JS:
- Object - is a collection of properties. Properties consist of a key and a value assigned to the key (value). Objects can store primitive types, arrays, functions (methods), and other objects.
- Arrays - they can store various types of data, the difference between an array and an object lies in the access to values, and that the array is iterable - you can use, for example, [loop] (../ petle.md) for.
- Functions - which are a block of code designed to perform a certain task.
All reference types are derived from the object Object
.
Typeof¶
JavaScript is not a strictly typed language, i.e. unlike Java, in JS we are not forced to specify a type when declaring. But sometimes we want to check what type of object "sits" under some [variable] (../ variables.md). The typeof
operator allows you to check the type of data, e.g.:
typeof 50; // number
typeof "Lorem ipsum"; // string
typeof []; // object
typeof function() {}; // function
Instanceof¶
The instanceof
operator checks to see if an object is an instance of another object.
[] instanceof Array; // true
[] instanceof Object; // true
Function instanceof Object; // true
"lorem ipsum" instanceof Object; // false
NOTE: In JS, names of objects most often start with a capital letter and use the camelCase convention.