JavaScript

Data Types in JavaScript? – Explained for Beginners

Every value is always distinct from each other, which categorizes them into various types. This categorization of various data is called Data Type. The reason for categorizing the data is to ensure how the data is being used within the program.

Like any other programming language, JavaScript also consists of various data types. Data Type is dynamic in JavaScript, which means a single value can be stored in various ways. For example:

let y; // Now y is undefined

y = 10; // Now y is a Number

y = "10"; // Now y is a String

In this article, we’ll discuss various data types in JavaScript with examples.

Types of Data in JavaScript

As discussed above, there are various data types in JavaScript, but all of them are categorized into three main categories:

  1. Primitive Type (String, Number, and Boolean)
  2. Composite or Reference Type (Object, Array, and Functions)
  3. Special Data Type (Null or Undefined)

Let’s discuss these types more thoroughly with examples.

Primitive Type

Primitive data types are simple basic blocks of any language, having only one value assigned to them at a time. They consist of:

  • Strings
  • Numbers
  • Boolean

String Data Type

A sequence of characters enclosed by single or double quotation are represented through string data type. The string consists of numbers, names, and quotes, as long as they are within the quotation mark.

Example

let x = "16"; // number stored as a string

var a = 'Hello World'; // using single quotation

var b = "My Name is.."; // using double quotation

var c = 'We\'re be very pleased.';// escaping single quote with backslash

Number Data Type

Various types of numbers, such as negative or positive numbers with decimal places, and numbers with exponential notations are represented through this data type:

Example:

var a = 5; // integer

var b = 25.5; // number with floating points

var c = 1.25e+6; // exponential form, similar to 1.25e6 or 1250000

var d = 1.25e-6; // exponential form, similar to 0.00000125

Furthermore, number data types also represent special values like Infinity, -Infinity, and NaN. Infinity is produced by dividing a non-zero with 0, producing a number greater than all. Whereas, NaN shows “not a number”. This occurs through invalid or undefined mathematical operations.

Example:

console.log(10 / 0); // Output: Infinity

console.log(-10 / 0); // Output: -Infinity

console.log(10 / -0); // Output: -Infinity

console.log("String" / 4); // Output: NaN

console.log(Math.sqrt(-1)); // Output: NaN

Boolean Data Type

Boolean data types represent the values of yes or no(on or off) through two specific values i.e true or false. Boolean values are used for various comparison purposes.

Example

var isEating = true; // yes, I'm eating

var isHungry = false; // no, I'm not hungry.

//comparison

var a = 1, b = 3, c = 5;

console.log(b > a) // Output: true

console.log(b > c) // Output: false

Output

Composite or Reference

Composite Data type helps us to store various data types and complex entities as a collection. They consist of:

  • Objects
  • Arrays
  • Functions

Objects Data Type

Objects help in storing various types of data as a collection. A particular key name is given to the object, which is a string. This can store various data types like numbers, boolean values and strings as a collection.

Example

var anObjectUndefined = {};

var user = {"name": "Sam", "secondName": "Mathew", "age": "25"};

// For better reading

var book = {

“bookName”: “Harry Potter and the deathly hallows”,

"author": "J.K Rowling",

"genre": "Fiction",

"published": "2007",

"total copies": 10

}

Arrays Data Type

Arrays in JavaScript are used to store various values in a single variable. Arrays can be of various types, depending on the type of data stored. Each value within an array is indexed, starting from 0. This way a user can access each element through arr[0] or arr[2] etc.

Example

var fruits = ["Banana", "Orange", "Peach", "Pineapple"];

var languages = ["English", "French", "Persian"];

console.log(fruits[1]); // Output: Orange

console.log(languages[2]); // Output: Persian

Output

Function Data Type

Functions are objects in JavaScript, with which a particular code is assigned. They are defined by using the keyword ‘function’ followed by a name given to them and parentheses enclosing parametric values. A function is then called and the lines it contains run and give an output.

Example

var greet = function(){

return "Hello To the Universe!";

}

// type of the variable greet

alert(typeof greet) // Output is a function

alert(greet()); // Output is Hello To the Universe!

Special Data Type

Special data type is:

Undefined Data type

It contains only a single value. This happens when a variable is not assigned with any value, so at the time of interpretation, the output is displayed as ‘undefined’.

Example

var x;

var y = "My Name is John"

 

alert(x) // Output: undefined

alert(y) // Output: My Name is John

Here, the variable x was not assigned with any value. That’s why the interpreter showed the result as ‘undefined’.

Null Data type

Similarly, the null data type also contains a single value. But, here in the case provided below, the value is assigned as “null”. So that it’s easily understood by the user that no value is assigned.

Example

var a = null;=
alert(a); // Output will be null

Conclusion

Data types are a major part of programming. It indicates which type of data the program is dealing with, whether it’s a number of various characters forming a string. In this topic, we explained various data types used in JavaScript and how they’re categorized. There are three main categories of data types in JavaScript and to store data specific types are present to store single as well as multiple data types.

Each type is explained with examples for better understanding and implementation. This makes storing and manipulation of data easier for the user.

About the author

Shehroz Azam

A Javascript Developer & Linux enthusiast with 4 years of industrial experience and proven know-how to combine creative and usability viewpoints resulting in world-class web applications. I have experience working with Vue, React & Node.js & currently working on article writing and video creation.