Introduction to JavaScript datatypes

Hassan Ali
5 min readDec 29, 2021

In this world of computers, the most important thing is data. Each day of our life we play with this data i.e creating data, deleting data, updating data. These are some of the most common operations we perform on our data.

Talking about data have you ever thought about how this data is stored in your computational devices? If you haven’t then let’s find out how.

Basically, the data is stored in the form of bits usually described as ‘Zeros’ and ‘Ones’ often known as binary numbers. In computers, they form high and low electrical charges or a shiny or dull spot on the surface of your hard-disk platters or a CD. Any piece of data can be broken down into bits. For example, we can convert the number 13 into bits. It’s the same phenomenon we use for decimal numbers, So in binary numbers, you have only 2 numbers instead of 10 counting from right to left. Following are the bits that make up the number 13, with weights given below them.

Datatypes

After understanding what is data and how it is stored in our devices now we will discuss its types in JavaScript.In general, there are two types of JavaScript datatypes:

  • Primitive Datatypes
  • Non-primitive Datatypes

Primitive Datatypes

“The types of data which are immutable are known as Primitive datatypes”

Primitive datatypes include all of the datatypes except one datatype known as “objects”. These kinds of data types are stored by value. Primitive datatypes include:

Numbers

The number type of data is based on numerical values. They are written just as same as we write them in our daily lives and we can write numbers as big as we can. For example

13 or 14 or 500

but Javascript uses a fixed number of bits, 64 of them, to store a single number value. So it means there is a limit to write different numbers. With N decimal digits we can represent 10NSimilarly, 64 binary digits can represent 264 different numbers, which is 18 quintillion. Further discussing numbers we will see two types of numbers that we have to deal with during our coding. First one is numbers with a decimal point and the second is bigInt. Javascript saves some of the bits in order to store the position of the decimal points.

9.81 or 8.90 or 1.4

For bigInt values, we can use exponent e which is a scientific notation for writing such numbers

2.567e8

Special Numbers

Unlike other numerical values considered as type “Number”, there are 3 such numerical values that are considered as numbers by JavaScript but do not behave like normal numbers. Two of them are Infinity and — Infinity both of them are not considered mathematically sound, and their computation leads to a third special number which is NAN

Which stands for “not a number” and you will get this special number when you try to perform calculations such as 0/0 (zero divided by zero) or Infinity-infinity.

Strings

Strings are a basic datatype that is used to represent text. They are written by enclosing the text/content in quotes. For example

“I am a String”

‘I am also a String’

`Am I also considered as a String?`

We can use single quotes, double quotes as well as backticks to mark strings but we need to make sure that the opening and closing quotes must be the same. Anything can be kept inside the quotations and JavaScript will consider it as a string but few characters are difficult to put between quotes. You can imagine how hard it will be to put a quotation between a quotation. Newlines can be included without escaping if the string is quoted between backticks.

To make it possible to include such characters in a string, the backslash is used and whenever a backslash is used inside a string it is considered that the next character behind it has a special meaning. Hence it is called an escaping character. You can use “\n” for the next line and “\t” for the tab character. For example

“I am the first line\n And this is the second line”

The output will be like

I am the first line

And this is the second line

Strings whether written with single quotes or double quotes behaves the same way but Backtick-quoted string also known as template literals can do more than a simple string. We can embed javascript code inside them which will be computed and calculated like normal and the value will become the part of the string. For example

`Three times 2 is ${3*2}`

And the result will be

“Three times 2 is 6”

JavaScript uses Unicode standards to model the string as a series of bits but here is a little complication: JavaScript uses 16 bits per string element which means 216different numbers which are quite less than what Unicode offers. Further, we will discuss how we can join two or more strings together. This is called “concatenation” this is done by placing a “+” sign between two strings. For example

“Hi my name is ” + “String”

Boolean

Javascript has a type of data that just has two values, true and false. Sometimes such values are useful to distinguish between two possibilities like ‘Yes’ and “No” or “On” and “Off”.

Non-primitive Datatypes

“Non-primitive Datatypes are those datatypes which are mutable

because they are stored by reference rather than value.”

The most used Non-primitive datatype in JavaScript is Object.

Object

“Group of values, other objects and the array is called an Object”

Previously we have talked about primitive data types which are considered atoms on which data structures are built but sometimes we need a much complex data structure to store our data. We can create an object by using a pair of curly braces. For example

Let product = {

{

“id”: 1,

“title”: “Fjallraven — Foldsack №1 Backpack, Fits 15 Laptops”,

“price”: 109.95,

“description”: “Your perfect pack for everyday use and walks in the forest. Stash your laptop (up to 15 inches) in the padded sleeve, your everyday”,

“category”: “men’s clothing”,

“rating”: {

“rate”: 3.9,

“count”: 120

}

}

We can add as many properties to our object.

Conclusion

I hope in this article you will get the optimum amount of information regarding javaScript datatypes. In this article, we have discussed how data is stored in a computer device and Primitive and non-primitive types of data of JavaScript.

--

--

Hassan Ali

A Web-developer with a vision to help other people of the world and advise them how they can become a better web developer.