Javascript Datatypes

Datatypes are the most important concept in a programming language. When you write a variable it has a particular type. This type is called Datatype. Javascript has 8 primary datatypes. They are:

  • string
  • number
  • bigint
  • boolean
  • undefined
  • null
  • symbol

In javascript, if you wrap something within a quote you will get a string. The quote may be a single or double quote. For example, Your name is John Doe. You want to write a variable that will print your name. How do you do that? See the code below.

const name = "John Doe";
console.log("My name is:", name);

The output will be

My name is: John Doe

How do you know what data type the name variable has? For that, you have to use typeof operator. If you use typeof before any variable it will say what the variable is. Use the console.log() method and print the result using typeof operator before the name variable.

const name = "John Doe";
console.log(typeof name);

The output will be

string

Now for example you are calculating the percentage of return today you have made so far from your investment in crypto. How do you do that? You have spent 1000 USD today and in return, you get 1050 USD. If I ask you what is the percentage of return you have made today? To do that you will first assign a variable to hold the investment. Let’s say a variable investment is declared and to it, the value 1000 is assigned. Then to calculate the percentage of return we will subtract the total revenue you have made today which is 1050 USD and assign the value of 1050 to a variable revenue. Then subtract investment from revenue and store it to a variable gain. Then calculate the percentage of return by multiplying the gain by 100 and dividing the value by 1000.

So the above explanation in code looks like this

const investment = 1000;
const revenue = 1050;
const gain = revenue - investment;
const percentageOfReturn = (gain * 100) / investment;
console.log("percentageOfReturn", percentageOfReturn);

The output will be

percentageOfReturn 5

Now that you have percetageOfReturn you want to know the data types. To do that type console.log(percetageOfReturn).

console.log(typeof percentageOfReturn);

The output will be

number

Now what is bigint data type. javascript can only handle integers up to 15 digits. More than that will be rounded up. What do I mean by that? Let me show you it in code.

let a = 999999999999999;
let b = 9999999999999999;
console.log(a);
console.log(b);

The output will be

999999999999999
10000000000000000

So if you see what is printed on the console is different from what we expect. We expect to see that value of b should be printed as 9999999999999999. We get 10000000000000000. That should not happen. Here javascript lost precision above any number that has more than 15 digits.

Add n to the end of b and run the program

let a = 999999999999999;
let b = 9999999999999999n;
console.log(a);
console.log(b);

The output will be

999999999999999
9999999999999999n

Check the type of a and b.

let a = 999999999999999;
let b = 9999999999999999n;
console.log(a);
console.log(b);
console.log(typeof a);
console.log(typeof b);

The output will be

999999999999999
9999999999999999n
number
bigint

Instead of adding n to the end of a number that is more than 15 digits long we convert the number to a string and apply BigInt to it. Check this out in the code.

let a = 999999999999999;
let b = 9999999999999999n;
let y = BigInt("9999999999999999");
console.log(a);
console.log(b);
console.log(y);
console.log(typeof a);
console.log(typeof b);
console.log(typeof y);

The output will be

999999999999999
9999999999999999n
9999999999999999n
number
bigint
bigint

We will discuss more on bigint later in this series. Now we discuss our next topic which is boolean. A boolean variable has a value of either true or false. For example, we will declare a variable switchOff which is assigned a value of false. We have also defined a value switchOn which is assigned a value true. Take a look

const switchOff = false;
const switchOn = true;
console.log(switchOff);
console.log(switchOn);
console.log(typeof switchOff);
console.log(typeof switchOn);

The output will be

false
true
boolean
boolean

So here we have printed the value of the two variables and we have also printed the data types of the two variables with typeof operator. We have seen that both are of the type of boolean variable.

Now we will discuss undefined types. For example, you have declared a variable and you have not assigned a value to it. You want to do some operation and after that, you want to assign a value to it and want to print that value to the console. Take an example with code.

let multXY;
const x = 3;
const y = 4;
console.log("multXY", multXY, typeof multXY);
multXY = x * y;
console.log("multXY", multXY, typeof multXY);

The output will be

multXY undefined undefined
multXY 12 number

So what’s going on? First, we have assigned multXY. we have printed out the value of it in the console. We have seen as there is no value assigned to this variable it’s now data type is undefined. As we have assigned a value of x * y to it, its data type is now number.

There is another important data type that is null. We can check whether a variable is null or not. We will declare a variable x and assign its value to be null. Later in this series, we will show how how to conditionally check whether variable weather is null or not, and depending on this we will decide what decision should we take. Let me show you a code example

let x = null;
console.log(x);

if (x == null) {
  console.log("x is null");
  //do other things depending on it
} else {
  console.log("x is not null");
  //do things depending on it
}

The output will be

null
x is null

So in the next article, we will discuss Javascript Functions.

Leave a Comment