Skip to content

Introduction

In JavaScript, data is divided into two categories:

  • primitive types - used to save simple data.
  • reference types - used to save complex objects.

All variables that are not of primitive type are reference types, e.g. objects or arrays.

Primitive types

The following primitive types are distinguished in JavaScript:

  • string - represents a sequence of characters.
  • number - represents a numeric value. In JS, number represents both integers and floating point numbers.
  • boolean - represents a logical value, true or false.
  • bigint - capable of holding an integer with a maximum value of more than the maximum value of the number.
  • undefined - a special representation of a value not declared, not existing in memory.
  • symbol - being a unique object identifier.

NOTE: in JS, strings can be created with the ' and " characters.

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. Objects can store primitive types, arrays, functions (methods), as well as other objects. * Arrays - can store different data types, the difference between an array and an object lies in the access to the value, and the fact that the array is iterable - for example you can use a for loop on it. * Functions - which are a code block created to perform a certain task.

All reference types are derived from the 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. However, sometimes we want to check what type of object is "sitting" under a certain variable. 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 whether 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, object names most often start with a capital letter and use the camelCase convention.