Understanding Data types in JavaScript

Understanding Data types in JavaScript

Data types specify what kind of data can we store and if it can be manipulated later. Data types in JavaScript look very simple. In this post, we’ll look at all the different data types that exist in JavaScript according to EcmaScript standards.

Data Types

There are eight basic data types in JavaScript which are divided into further two categories.

  • Primitive
  • Non-Primitive (or Composite)

JavaScript being a dynamic language, we don’t need to specify the type of variable. The var keyword can store different data types over time. Primitive data types can hold only one value at a time, whereas non-primitive data types can hold a collection of values. Below is a list of data types that JavaScript can have:

Primitive

  • Number
  • BigInt
  • String
  • Boolean
  • Undefined
  • Symbol
  • Null

Non Primitive

  • Object

Let’s discuss more on each of the above types.

1. Number

The number data type can be an integer, floating value, exponential value, NaN, +Infinity/-Infinity. We can use operators for numbers such as multiplication * , division /, addition +, subtraction -, and others.

var a = 120    //integer value
var b = 120.50 //float value
var c = 120/0  //Infinity
var d = x / 10 //NaN

The number data type also includes some special values, Infinity, -Infinity, and NaN. NaN - *Not a number.* Performing any mathematical calculations is safe in JS. Worst case it’ll be NaN but the script will never stop.

2. BigInt

BigInt type was recently added to JavaScript to represent integers that are beyond the safe limit for numbers. It is created by appending n to the end of an integer or by using BigInt() and giving it an integer or string value.

const integer = 2n ** 53n // 9007199254740992n
const bigNum = BigInt(9007199254740991)
// ↪ 9007199254740991n
const bigIntString = BigInt("9007199254740991")
// ↪ 9007199254740991n

typeof BigInt(1) // bigint

3. String

The string data type can hold any text enclosed by single or double-quotes. We can use the String() function to create a new string. It can also be used for converting a non-string to a string.

var a = "Let's have a cup of tea."; // single quote inside double quotes
var b = 'Sure, let\'s go';  // double quotes inside single quotes
var c = new String("String object"); // creating new string

We can access characters of string by using charAt() method, to append multiple strings together we can use the +/- operator.

var a = 'CAT'.charAt(2);  // a = T
var b = "Lorem ipsum, dolor sit amet" + "," + "consectetur adipiscing elit"
// "Lorem ipsum dolor sit amet, consectetur adipiscing elit"

4. Boolean

The boolean data type has only two values, true and false. Primarily used to check logical a condition. It can be used to check logical comparisons of two variables.

typeof(true)    // boolean
typeof(false)   // boolean

var a = 5, b = 6
var check = Boolean(a < b) // true

5. Symbol

The symbol is used to create unique identifiers for objects. Every Symbol() is guaranteed to return a unique symbol. We can use Symbol.for('key') to return same Symbol for a given value of the key.

const symbol1 = Symbol('foo');
const symbol2 = Symbol('foo');

symbol1 === symbol2 // false

Since it creates a new Symbol each time when we compare two symbols.

6. Undefined

The undefined data type can only have one value, undefined. If we declare a variable and don’t assign any value, it’ll have the value undefined. If we don’t return anything from the function, it’ll be undefined.

let name;
console.log(name);            // undefined
console.log(typeof name);     // undefined

7. Null

This is a special data type that can have a null value. It determines that there is no value. A variable can be explicitly emptied of its current value by assigning it the null value.

var a = null;
a; // null

if(a) {
    // Will not be executed, since value of a is null
}

When null is used in conditionals, it is treated as false for boolean operations.

8. Object

The Object is a complex data type that allows to store collections of data. The object type refers to the collection of primitive as well as complex data types. Object properties exist in key: value pairs, separated by commas. Property values can be values of any type, including other objects, thus these are wonderful to build complex data structures. A key value is a String or a Symbol value.

var obj1 = { a: 5, b: 6 };

When we refer to obj1, we actually refer to the address in memory that contains the value { a: 5, b: 6 } instead of the value. All objects in JavaScript are instances of Object. The Object constructor creates an object for any given value, if the value is undefined or null, it’ll create an empty object.