Signup/Sign In

JavaScript Data Types

JavaScript Data types are used to identify the type of data that is stored inside a variable during the script execution. As we have already specified about the Dynamic Typed JavaScript feature so we do not have to specify the data type of the variable while declaring it.

So JavaScript data types are basically for identification purposes to know what is being stored in the variable, when it's stored, not before that.

Dynamically typed language is a language that infers data types at runtime. It allows storing different types of values to a variable during programming.

Below we have a basic example, where we have defined a variable with a numeric value and then updated it to store a string value. So JavaScript allows this.

var x = 5;    // x is a number
x = "studytonight";    // x is string here.

JavaScript broadly supports three types of Data types, they are:

  • Primitive Type

  • Reference Type

  • Special Data Type

Let's cover each one of this one by one while seeing which category has all data types.

JavaScript Primitive Data Type

JavaScript Primitive data types can be classified further into the following types:

  1. String Data Type

  2. Boolean Data Type

  3. Number Data Type

These are the most common data types, used for storing sequences of characters, true/false and numeric values respectively. Let's cover them one by one with examples.

1. JavaScript String Data type

Whenever we write a character or sequence of characters inside single or double quotes then it becomes String. For example "StudyTonight".

We can use both single or double-quotes to create string type values.

var str = "Ferari F60";  // string using double quotes

var str1 = 'Volvo S60';  // string using single quotes.

To have a single quote as part of the string we should use the double quote to enclose the string value and vice versa. And if you want to include a single quote in the string which is defined by enclosing it within single quotes only, in that case, we must use a backslash \ to escape the single quote, and similarly, we can escape the double quote too in a string value.

Let's see an example of this:

var str1 = "Ferari's F60";    // Output: Ferari's F60

var str2 = 'Volvo "S60"';   // Output: Volvo "S60"

var str3 = 'Ferari\'s F60';    // Output: Ferari's F60

We have covered JavaScript String in details and also covered various String methods in JavaScript.

2. JavaScript Boolean Data type

JavaScript Boolean Data type is used in conditional based programming. It can have two values, either true or false.

var isOn = true;  // bulb is on

var isOn = false;  // bulb is off

We get Boolean values while comparing two numbers, for example:

doument.write(4 < 2)  // false
doument.write(4 > 2)  // true

We will see this in the JavaScript If else Flow Control tutorial.

3. JavaScript Number Data type

JavaScript Number Data type can be with or without decimal points and can have negative and positive values.

var x = 45; // Number without decimal point

var y = 45.90; // Number with decimal point - floating point

var z = -10; // Number with negative value

The JavaScript Number data type also represents some special values like Infinity, -Infinity and Nan. When a positive number is divided by zero (it's a popular case of runtime error), in JavaScript it's represented as Infinity. Similarly, when a negative number is divided by zero we will get -Infinity.

var a = 100;
var b = -100;
var c = 0;

alert(a/c);  // Infinity 
alert(b/c);  // -Infinity

And Nan means Not a number, if we try to perform any operation between a numeric value and a non-numeric value like a string we will get this as output.

var a = "Studytonight";
var b = 7;

alert(a/b);    // Nan

JavaScript Composite Data types

These data types can hold collections of values and more complex entities. It is further divided into Object, Array and Function.

  1. Object data type

  2. Array data type

  3. Function data type

1. JavaScript Object Data Type

In JavaScript, an object data type is used to store the collection of data. Object's properties are written as key:value pairs, which are separated by commas and enclosed within curly braces {}.

The key (name) must always be a string, but the value can be of any data type. This is pretty similar to a map data structure in many programming languages which also stores key-value pairs like this.

var name = { };   // It will create an empty object.

var emp = {firstname="ram", lastname="singh", salary=20000};

We have covered JavaScript Objects in a separate tutorial with examples.

2. JavaScript Array Type

JavaScript Array data type is written inside a pair of square brackets [] and is used to store multiple values of the same datatype be it strings, numbers etc. The items in a JavaScript array are also written in a comma-separated manner.

Each element in the array gets a numeric position, known as its index. The array index starts from 0 or we can say that array indexes are zero-based, so that the first array element is arr[0] and not arr[1].

Let's take an example for JavaScript array:

// Creating an Array
var cars = ["Ferrari", "Volvo", "BMW", "Maseratti"];

We have covered JavaScript Arrays in a separate tutorial with examples.

3. JavaScript Function Type

You must be thinking, how a function can be a datatype. But in JavaScript functions act as a data type which can be assigned to a variable. JavaScript Function is nothing but a set of statement inside a code block which is used to perform a specific operation and this datatype is of callable in nature. So, you can call it anywhere in the program whenever needed.

Since functions are objects, so it is possible to assign them to a variable.

Functions can be stored in variables, objects, and arrays. Functions can be passed as arguments to other functions too and can be returned from other functions as well.

var welcome = function() {
                  return "Welcome to StudyTonight!";
              }

We have covered JavaScript Functions in a separate tutorial with examples.

JavaScript Special Data types

JavaScript also has some special data types, although seeing function called as a data type would have already been special for you. But there are two more.

  1. Undefined Data type

  2. Null Data type

Let's cover each of them one by one.

1. JavaScript Undefined Data Type

When a variable is just declared and is not assigned any value, it has undefined as its value. Yes, undefined is a valid data type in JavaScript and it can have only one value which is undefined.

We can even use this value while doing some comparison. Let's take an example:

var a;   // Undefined

alert(a == undefined);   // returns true

2. JavaScript Null Data Type

JavaScript Null data type is used to represent no value. It is not similar to undefined, and neither it is similar to empty value or zero value. The Null datatype means, the variable has been defined but it contains no value.

The Null data type can have only one value, which is null. Let's take an example for this:

var a = null;

alert(a);    // Output will be null

JavaScript typeOf Operator

The typeOf operator in JavaScript can be used to check the data type of any variable. Although its an operator but we are mentioning it here, because it's oftenly used to check the data type of any variable.

Let's take a few examples to see how this works:

// Function datatype
var welcome = function() {
                  return "Welcome to StudyTonight!";
              }

typeOf welcome;    // function

var a = null;

typeOf a;   // null

// Array datatype
var cars = ["Ferrari", "Volvo", "BMW", "Maseratti"];

typeOf cars;    // array

You can try this with other data types too.

Conclusion:

In this topic, we explained data type of variables used in the javascript. all the topics and subtopics are explained with live example so that we can understand the topic in better manner.



About the author:
I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development. Founder @ Studytonight