JavaScript Data Types

Data types are the different classification given to data based on the type of information it can contain. In simple words, JavaScript data types give information on what type of data we are using in our program.

In JavaScript, data types is simply divided into two categories:

Primitive Data Types

Primitive Data Types stores only one type of data. Thus, they are simple to understand and represent as compared to Objects. They are further divided into 7 categories:

In this lesson, we will use variables to explain about the different Data Types.


JavaScript Number Data Type 🔢

The Number Data Type in JavaScript represent numbers, including numbers with decimal points & negative values. These numbers with decimal points are called floating values (1.23, 0.76, 12.4647).

Examples of Number Data Type: 0, 9, 1367, 5.76, 0.09237, -45, etc.

Example

We are going to use Variables and Constants throughout our examples. You can learn about it here.

var age = 12; //Normal number
var gravity = 9.807; //Floating value
var temperature = -107; //Negative value

console.log(age);
console.log(gravity);
console.log(temperature);

Output

12
9.807
-107

Example: Extra large numbers can be represented in scientific notation and will still be considered as a Number Data Type.

let num1 = 6789e5; //e5 means 5 zeros after 6789. In other words: 6789 * 10^5
let num2 = 6789e-6; //e-6 means 6 digits after decimal point .

console.log(num1);
console.log(num2);

Output

678900000
0.006789

Example: Even Infinite values and NaN (Not a Number) are considered to be a Number Data Type.

let num1 = 23/0; //The result of 23/0 is Infinity
let num2 = "apple"/2; //The result is NaN

console.log(num1);
console.log(num2);

Output

Infinity
NaN

JavaScript String Data Type 🔡

A String in JavaScript is a series of characters wrapped in double quotes ” “ or single quotes ‘ ‘. The String Data Type in JavaScript represents Strings.

Examples of String Data Type: “abc”, ‘apple’, “Hi, My Name is Bitslord”, ‘My age is 20. I live on the moon.’, etc.

Example

var name = 'John Doe'; //Using single-quotes
var message = "I love programming." //Using double-quotes

console.log(name);
console.log(message);

Output

John Doe
I love programming.

Example: We can also put quotes inside quotes as long as the quotes inside do not match the quotes outside.

var bio = 'My name is "John".'; //Using double-quotes inside single-quotes
var message = "I love 'JavaScript'." //Using single-quotes inside double-quotes

console.log(bio);
console.log(message);

Output

My name is "John".
I love 'JavaScript'.

JavaScript Boolean Data Type 👎👍

A Boolean Data Type can represent only two possible values: true and false. Computers can only understand binary which has only two values: 0 and 1, where 0 means Off and 1 means On. In the same way, true means On while false means Off.

Booleans are useful to answer “Yes” or “No” questions in programming. We will learn more about this in our JavaScript If Else lesson.

Example

var isSunHot = true;
var isMoonAStar = false;

console.log(isSunHot);
console.log(isMoonAStar);

Output

true
false

JavaScript Null Data Type ⛔

The Null Data Type represents the absence of a value in a program. It is represented by the keyword null.

Example

var boxOfChocolates = null;

//It shows that the boxOfChocolates variable is empty
console.log(boxOfChocolates);

Output

null

JavaScript Undefined Data Type ⁉

The Undefined Data Type in JavaScript represents the value that has not been assigned. A variable which does not have a value will automatically be assigned with an undefined value.

Example

var number;

//Will display "undefined" as the variable "number" does not have a value
console.log(number);

Output

undefined

JavaScript BigInt Data Type 📏

A BigInt Data Type in JavaScript represents numbers larger than 253-1 which is the largest number JavaScript can represent using the Number Data Type. A BigInt Data Type value can be created by adding the letter n at the end of an integer.

Example

var largeNumber = 9007199254740991n; //Adding the letter n to change the integer into BigInt Data Type

console.log(largeNumber);

Output

9007199254740991

JavaScript Symbol Data Type ⚛

A Symbol Data Type represents unique identifiers. A value having a Symbol data type can be created by using the Symbol( ) function. We will learn about functions in the lesson JavaScript Functions later on.

Example

var firstSymbol = Symbol('apple');
var secondSymbol = Symbol('apple');

Here, although both firstSymbol & secondSymbol identifiers have the same value ‘apple’, these both will be treated as variables having different values because Symbol represents each identifier as unique.


These are all the primitive data types included in JavaScript. Apart from primitive data types, JavaScript has one more data type – Object. Let’s learn about that below.


JavaScript Non-Primitive/Object Data Type

An Object in JavaScript is a collection of related data. It is a complex Data Type which allows us to store more than one type of primitive data as key-value pairs.

We can create objects by using curly-braces { }. Do not worry, if you do not understand Objects right now. We will learn about it in our JavaScript Objects lesson later on.

Example

//dog is an object whose properties are name, breed, age and hasTail.
//As you can see, the dog object can store multiple values having multiple data types  
var dog = {
	name: 'Tommy', //String Data Type
	breed: 'German Shepard', //String Data Type
	age: 4, //Number Data Type
	hasTail: true //Boolean Data Type
};

console.log(dog);

Output

{ name: 'Tommy', breed: 'German Shepard', age: 4, hasTail: true }

Type Checking in JavaScript

We can check the Data Type of any data that we are using in our program by using the typeof operator. We will learn about Operators later in our JavaScript Operators lesson.

Syntax

The typeof operator returns the data type of the value that we are checking.

typeof value //Returns the data type of value

Example

var age = 30;
var name = 'Mr. Bitslord';
var hasBlueEyes = false;
var bankBalance = null;

console.log(typeof age);
console.log(typeof name);
console.log(typeof hasBlueEyes);
console.log(typeof bankBalance);

Output

number
string
boolean
null

In this way, Data Types are categorized in JavaScript. You will later on use most of these data types on the programs that you write.