JavaScript Data Types

In this section of the tutorial, we will discuss the Data Types in JavaScript and their uses which is important to define the type of value that is to be assigned in a variable.

Now let’s move further and understand the definition of Data Types in JavaScript with some real-world examples.

What are Data Types in JavaScript?

Data is generally referred to be as information, and information can be of very diverse types.

For example, in an online banking system, a customer may request details of the transactions made in a month. Here, the system will filter out all the irrelevant data that are not related to transactions and then display accurately the data desired by the customer on the webpage.

  • JavaScript deals with data such as numbers and text, following some fundamentals as any other language.
  • JavaScript uses Data Types to define the type of value that is to be assigned in a variable used in the code.
  • JavaScript manipulates and processes different data types and it is stored in the computer’s memory for reusability throughout the program.

We will further look into the Data Types in JavaScript and understand them with few more profound examples.

Types of Data in JavaScript

A computer processes and displays information or data. These data are further modified, interpreted, or filtered in some way by the computer.

In JavaScript, data can be found of many different types. JavaScript handles data types such as numbers and text which is also used extensively in the real world, and data types like object data type are abstract and are mostly used in making coding easier.

  • JavaScript is a weakly typed language and it is forgiving in nature in terms of how a user uses different data types in a program.
  • JavaScript does not require a user to declare the data type of the data, thus making it data type error-free, unlike other strong typed languages.
  • However, in some situations, we need to explicitly point to JavaScript about what sort of data we want and how it should be processed to avoid confusion.

We will understand the above-given point using a code example for better understanding.

Example:

Code:

var x = 2;
var y = 4;

console.log(x+y);

Output:

6
  • Here, var keyword is used to declare variables x and y respectively.
  • Variables x and y hold a number value, i.e. 2 and 4 respectively.
  • console.log() is used to display the result.
  • We can see that the numbers are directly assigned to the variables without specifying their data type.
  • Number values are added and displayed in the console.

But if we assign string values in the variables, JavaScript automatically concatenates the two strings directly by summing up the variables.

Code:

var x = "WELCOME TO ";
var y = "CODEDEC";

console.log(x+y);

Output:

WELCOME TO CODEDEC
  • Here, var keyword is used to declare variables x and y respectively.
  • Variables x and y hold string values, i.e. “WELCOME TO” and “CODEDEC” respectively.
  • console.log() is used to display the result.
  • We can see that the string values are directly assigned to the variables without specifying their data type.
  • String values are concatenated or joined together and displayed in the console as a whole.

So, from the above two code examples, we can understand a very unique feature of JavaScript, where it automatically recognizes what is to be done with the type of data in the variables.

Now, we will discuss some most commonly used data types in JavaScript which will be used extensively throughout the tutorial.

Numerical Data

Numerical Data are of two main types, i.e. integer and floating-point numbers.

Integers

  • These consist of whole numbers, such as 255,2,-97. These numbers can be positive integers or negative integers having a very wide range such as -3^(54) to 3^(54).
  • It also includes assigning an octal literal, the number consisting of the first digit as zero (octal literal) with octal digit (0 to 7).

Example:

Floating-point numbers

  • These consist of numbers in fractional forms which can either be positive or negative and can lie in a massive range of numbers, such as 2.164.
  • While representing numbers that consist of zeroes after the decimal, JavaScript ignores the zeroes after the decimal.
  • And for a very large number or a small number, JavaScript permits the use of ‘e’ such as 2.32e7.

Example:

Text Data or String Type Data

  • Text data can also be understood as String data if there is a combination of more than one character in a text.
  • JavaScript can recognize a string data if it is enclosed inside quote marks, e.g. “CODEDEC”.
  • For a string containing a single quote in it, we can either use a double quote to enclose the string or we can use a backslash before the single quote to escape the single quote, e.g. “I\’m Home”
  • We can use an addition + sign to join two strings together in JavaScript, e.g. “WELCOME” + “TO” + “CODEDEC”.

Example:

There are few situations in JavaScript where we need to use escape sequences to avoid confusion to the JavaScript engine.

Below is a table that briefly lists some escape sequences and the character they represent.

Escape Sequences

Character Represented

\b

Used for Backspace

\f

Used for Form Feed
\n

Used for New Line

\r

Used for Carriage Return
\t

Used for Tab

\’

Used for Single Quote
\”

Used for Double Quote

\\

Used for Backslash
\xNN

NN is a hexadecimal number that specifies a character in Latin-1 character set

Boolean Data

  • Boolean Data Types used the concept of true or false which is fundamental to the digital computers where they don’t understand maybes, only one of either true or false.
  • Similarly, JavaScript uses the same concept of distinguishing between two facts, i.e. by using two possible values: true for yes and false for no.
  • Boolean Data Type enables JavaScript to make decisions based on the nature of the answer.
  • JavaScript accepts the usage of true or false in lowercase only.

Example:

In the next section of the tutorial, we will discuss JavaScript Operators and its uses which is important to perform mathematical and logical operations with the values assigned in a variable and compute a result.