Data types in JavaScript


Reading time: 30 minutes | Coding time: 10 minutes

Javascript is a dynamic language. the variables in javascript are not hardly coupled with any specific data type like C, where an int data type variable can only hold integers. This is called being a loosely typed language.

A variable can be assigned (and re-assigned) values of all types.

let value = "OpenGenus";     // string

Now if we assign a value 98 to this variable. It will work fine in javascript with no errors.

value = 98;             // integer/Number

Types of Data types in Javascript

According to the latest ECMAScript standard, there are eight data types defined in javascript. Seven of these data types are primitive:

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

and

  • Object

Primitive Values

these are the values which cannot be changed. Do not confuse a primitive itself with a variable assigned a primitive value. The variable can be reassigned new value but existing values can not be changed or mutated.

As you can change a value in array like -

let a = ['a','b','c','d'];
a[2] = 'z';
console.log(a);

// [a,b,z,d]

but we can not do the same operation with strings -

let name = 'John';
name[2] = 'p';
console.log(name);

// John

Boolean type

Boolean represents a logical entity and has two values -
true
false
Many comparison operations == === < > (and so on) return either one or the other. if , while statements and other control structures use booleans to determine the flow of the program.
They don't just accept true or false, but also accept truthy and falsy values.
Falsy values, values interpreted as false, are

0
-0
NaN
undefined
null
'' //empty string

All rest is considered a truthy value.

const val = 1 > 2;
console.log(val);         // false

console.log(1 == 9);      // false

console.log(2 !== 4);     // true

Null type

The null type has only one value -
null

Null value represents a reference that points to a non existent or invalid address or zero.

const val = null;
console.log(val);         // null

console.log(val + 2);     // 2

console.log(val * 2;      // 0
  • Undefined type -

Undefined as the name suggest a value which is not defined. A variable that has not been assigned a value get the value undefined.

let val;
console.log(val);

// undefined

It's commonly returned by functions with no return value. When a function accepts a parameter but that's not set by the caller, it's undefined.
To detect if a value is undefined , we use the construct:

typeof variable === 'undefined'

Number type

Number type can be represent two types of literals -
Integers literal and floating point literal.
It can hold numbers between -(253 - 1) and (253 - 1)
Number type has 3 symbolic values -
+Infinity
-Infinity
NaN (Not a number)

Internally, for javascript every number is a float.

Integers

10
48383459340
0xCC //hex

Floats

3.14
.1222
12.2e4

BigInt type

It is also a numeric type in javascript which can represent integers with more precision.
With the help of BigInts, we can store and operate on very large integers beyond the integer limit for Number type.

BigInt is created by adding/appending n to the end of an integer.

const value = 52342424123131312n * 235678954n;
console.log(value);

// 12336007767163954816807648n

BigInt can not be used interchangibly with Numbers. It throws an error.

const value = 5234 * 235678954n;
console.log(value);

// TypeError: Cannot mix BigInt and other types, use explicit conversions

String type

Javascript string is used to represent text data. String is anything/any data enclosed within double quotes ( " " ) or single quotes ( ' ' ).

let a = "Red12@#";
console.log(a);

// Red12@#

Template strings

Introduced in ECMAScript 2015, template strings are string literals that allow a more powerful way to define strings. It uses backticks in place of quotes.

let name = `John`;

It makes it easier to operate with strings. Like -

console.log( `User name is ${name}`);
// User name is John

here ${name} is javascript variable from the above example.

Symbol type

Symbol is new data type introduced with ECMAScript 2015. A symbol is unique and immutable primitive value.

Once we create a symbol, its value is kept private and for internal use.
We can create a symbol by calling the Symbol() function

const mySymbol = Symbol()

Object type

Anything that is not primitive type is an object type.
Functions, arrays and what we call objects are object types. They are special on their own, but they inherit many properties of objects, like having properties and also having methods that can act on those properties.

var person = {
  firstName : "John",
  lastName  : "Doe",
  age     : 50,
  eyeColor  : "blue"
};

With this article at OpenGenus, you have the complete idea of data types and objects in JavaScript. This is a fundamental concept and will lay the foundation of mastering JavaScript.