Is it possible to create a new data type in JavaScript?


The task we are going to perform in this article is it possible to create a new data type in JavaScript. For instance, let’s consider we have variable student −

var student = "username"
console.log(typeof student)

Now, I want to declare the type of that variable and print that in the output, and it will look like this −

Output:"string"

Let’s dive into the article to learn more about creating a new data type in JavaScript.

Yes, it is possible to create a new data type in JavaScript, you can use the concept of Class. If you want to check the actual data type, then you can use instanceof operator.

Classes in JavaScript serve as a model for building things. Code is used to encapsulate and manipulate data. Classes in JS are based on prototypes, but they also include some syntax and semantics that are distinct from those of ES5 classes.

The instanceof operator tells about the data type. Here is the sample JavaScript code which will give a brief description about how to create a new data type and how to check the data type. Here, I will give the custom implementation to check the data type.

For getting a better understanding on creating a new data type in JavaScript, let’s look into the following examples.

Example

In the following example, we are running the script using the instanceof operator to check the type of function.

<!DOCTYPE html>
<html>
   <body>
      <script>
         class car {
            constructor(color, old) {
               this.color = color;
               this.old = old;
            }
         }
         const value = new car("value", 400);
         function checkType(data) {
            if (data instanceof car) return "car";
            return typeof data;
         }
         document.write(checkType(value) + " < br > ");
         document.write(checkType("value") + " < br > ");
      </script>
   </body>
</html>

When the script gets executed, it will generate an output consisting of the type of function displayed on the webpage that was triggered by the event that got triggered on executing the script.

Example

Consider the following example, here we are using the Object type to create a new data type and also detect the type of data in JavaScript.

<!DOCTYPE html>
<html>
   <body>
      <script>
         function Student() {}
         const student = new Student()
         const getObjTag = (obj) => Object.prototype.toString.call(obj).slice(8, -1)
         document.write(getObjTag([]) + " < br > ")
         document.write(getObjTag(new Set([])))
      </script>
   </body>
</html>

On running the above script, the output window will pop up, displaying the data type on the webpage obtained by an event that gets triggered on running the script.

Example

Let’s look into the following script, where we are running the script to get the data type in JavaScript.

<!DOCTYPE html>
<html>
   <body>
      <script>
      class Car {
         constructor(color, model, price) {
            this._color = color;
            this._model = model;
            this._price = price;
         }
         info() {
            return `The color of the car is ${this._color} and, its model is ${this._model}. It costs arround ${this._price}`
         };
      };
      const car = new Car("black", 2022, "4cr");
      document.write(car.info(), " < br > ")
      document.write("Type of the object: ", typeof car)
      </script>
   </body>
</html>

When the script gets executed, the event gets triggered and displays a text along with a data type on the webpage.

Example

Execute the below to observe how to create a new data type in JavaScript.

<!DOCTYPE html>
<html>
   <body>
      <script>
         class Game {
            constructor(gameName) {
               this.gameName = gameName;
            }
         }
         const ticTacToe = new Game("TicTacToe");
         function dataTypeBelongsTo(object) {
            if (object instanceof Game) return "Game";
            return typeof object;
         }
         document.write("The ticTacToe is the object of Game class: " + (ticTacToe instanceof Game) + " < br > ");
         document.write("The data Type of ticTacToe is: " + dataTypeBelongsTo(ticTacToe) + " < br > ");
         document.write("The data Type Candy Cash is: " + dataTypeBelongsTo("Cady Cash"))
      </script>
   </body>
</html>

On running the above script, the output window will pop up, triggering the event and displaying a data type on the webpage.

Updated on: 21-Apr-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements