Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Is it possible to create a new data type in JavaScript?
The task we are going to perform in this article is to explore whether it's possible to create a new data type in JavaScript. For instance, let's consider we have a variable student:
var student = "username"; console.log(typeof student);
string
Now, I want to declare a custom type for that variable and print that in the output. Let's dive into the article to learn more about creating a new data type in JavaScript.
Can You Create Custom Data Types?
Yes, it is possible to create custom data types in JavaScript using classes. If you want to check the actual data type, you can use the instanceof operator.
Classes in JavaScript serve as templates for creating objects. They encapsulate data and methods to manipulate that data. Classes in JS are based on prototypes but provide cleaner syntax than traditional ES5 constructor functions.
Using Classes to Create Custom Types
Here's how to create a custom data type using a class and check its type:
<!DOCTYPE html>
<html>
<body>
<script>
class Car {
constructor(color, year) {
this.color = color;
this.year = year;
}
}
const myCar = new Car("red", 2023);
function checkType(data) {
if (data instanceof Car) return "Car";
return typeof data;
}
document.write("Type of myCar: " + checkType(myCar) + "<br>");
document.write("Type of string: " + checkType("hello") + "<br>");
document.write("Is myCar a Car? " + (myCar instanceof Car));
</script>
</body>
</html>
Type of myCar: Car Type of string: string Is myCar a Car? true
Using Constructor Functions
You can also create custom types using constructor functions:
<!DOCTYPE html>
<html>
<body>
<script>
function Student(name, grade) {
this.name = name;
this.grade = grade;
}
const student = new Student("John", "A");
function getCustomType(obj) {
if (obj instanceof Student) return "Student";
return typeof obj;
}
document.write("Type of student: " + getCustomType(student) + "<br>");
document.write("Student name: " + student.name + "<br>");
document.write("Is student a Student? " + (student instanceof Student));
</script>
</body>
</html>
Type of student: Student Student name: John Is student a Student? true
Detecting Object Types
For more precise type detection, you can use Object.prototype.toString:
<!DOCTYPE html>
<html>
<body>
<script>
class Game {
constructor(name) {
this.name = name;
}
}
const game = new Game("Chess");
const getObjectType = (obj) => Object.prototype.toString.call(obj).slice(8, -1);
document.write("Array type: " + getObjectType([]) + "<br>");
document.write("Set type: " + getObjectType(new Set()) + "<br>");
document.write("Game type: " + getObjectType(game) + "<br>");
document.write("Using instanceof: " + (game instanceof Game));
</script>
</body>
</html>
Array type: Array Set type: Set Game type: Object Using instanceof: true
Complete Example with Methods
Here's a comprehensive example showing a custom data type with methods:
<!DOCTYPE html>
<html>
<body>
<script>
class BankAccount {
constructor(owner, balance) {
this.owner = owner;
this.balance = balance;
}
deposit(amount) {
this.balance += amount;
return this.balance;
}
getInfo() {
return `Account owner: ${this.owner}, Balance: $${this.balance}`;
}
}
const account = new BankAccount("Alice", 1000);
function identifyType(obj) {
if (obj instanceof BankAccount) return "BankAccount";
return typeof obj;
}
document.write("Account info: " + account.getInfo() + "<br>");
document.write("Type: " + identifyType(account) + "<br>");
document.write("After deposit: $" + account.deposit(500));
</script>
</body>
</html>
Account info: Account owner: Alice, Balance: $1000 Type: BankAccount After deposit: $1500
Key Points
- Use
classor constructor functions to create custom types - Use
instanceofoperator to check if an object belongs to a custom type -
typeofwill return "object" for all custom types - Custom types can have properties and methods like built-in types
Conclusion
JavaScript allows creating custom data types using classes or constructor functions. Use the instanceof operator to identify custom types, as typeof returns "object" for all custom instances.
