Data Types in Javascript

  • Post category:Javascript
  • Reading time:5 mins read

In this post, you will learn about the different Data Types in Javascript. In javascript, we do not need to manually define the data type of value that we are storing in a variable. It is determined automatically in this language.

In javascript, every value is either an object or primitive. A value can be only primitive when it is not an object.

Let’s understand this first by using some examples.

Object Example:

Navigate to Javascript topics to know more about objects.

let myLocation = {
    city: "moscow"
};

Primitive

let myCity = "berlin";
let number = 26;

Now, let’s focus on primitive data types in javascript.

Data Types in Javascript

There are seven primitive data types in Javascript.

1. Number

We can use this data type for any number value such as integers, decimals, etc.

let number = 26.5;  // It will give output as 26.5
let value = 20;     // It will give output as 20

2. String

A string is a text value that should be in double quotation marks (” “) or single quotation marks.

let city = "xyz";

3. Boolean

Boolean is either true or false. It is used in logical conditions for taking decisions and declaring a variable either true or false. We can declare Boolean data type like this.

Do not write Boolean values in quotation marks as these values are not strings.

let isPresent = true;
let isPresent = false;

4. Undefined

An undefined data type is when a value required by the variable is not there in the code or you can say the value is not defined for that particular variable. So, in such cases, you can see undefined using console.log(). Let’s see an example.

let myCity;
console.log(myCity);

let favouriteNumber;
console.log(favouriteNumber);

Output: undefined

The output for both will be undefined.

5. Null

Null in javascript is an empty value. So, it represents the absence of any object. We can use this for boolean operations as the null value is a falsy value in Javascript. You can navigate to Javascript Topics to know more about this.

let k = null;

We have declared k with the value null.

if (null) {
  console.log("Hi");
} else {
  console.log("Bye");
}
Output: Bye

In the above code, I have used null in the if condition. So, as I said null is a falsy value so it will now go to the else condition and it will print the output Bye.

6. Symbol

Symbol data type was introduced in 2015. It defines a value which is unique and that can not be changed. You can create a Symbol by writing Symbol().

Each time when you write Symbol() in your code, it will then create a new Symbol each time. Let’s understand using an example.

let p = Symbol();
let q = Symbol("code");
let r = Symbol("code");
let s = Symbol();
console.log(q == r);
console.log(p == s);
Output: false, false

In the above code, I did an equality comparison and in the output, you can see it is giving us false. As I mentioned, each time when we write Symbol() it will create a new Symbol.

7. BigInt

This data type was introduced in 2020 (Ecma Script 2020). It is used to define large integers in Javascript. A BigInt is created by adding n to the end of an integer. So, using this, you will be able to store big numbers.

You can use operations such as +, -, %, *, etc with BigInt as well.

Note – BigInt is loosely equal to a Number.

const x = BigInt(140 * 400);
console.log(x);
Output: 56000n

Checking Data types of Values using typeof Operator

Now, let’s see some examples to check the type of values using typeof operator.

We use typeof operator to check the type of values such as number, string, boolean, etc.

let country = true;
console.log(typeof true);
console.log(typeof country);

Output:

Boolean
Boolean

The output for both is Boolean. Country variable stores true value so it is a boolean. That’s why in the console it is showing Boolean.

let country = "Japan";
console.log(country);

let secondCountry = australia;
console.log(secondCountry);

Output:

Japan
Uncaught ReferenceError: australia is not defined

The second console statement is throwing an error because the word australia is not written in ” “. So, Javascript is considering it as a variable that is not defined yet.

console.log(typeof 8);

Output:

number
Share this