Saturday, October 18, 2025
HomeLanguagesJavascriptWrite a program to calculate salary in TypeScript

Write a program to calculate salary in TypeScript

In this article, we will understand how we could create a basic Salary Calculator in TypeScript by using simple mathematical logics and operators (like divide(“/”) or multiply (“*”) or addition (“+”)).

Syntax: Following is the syntax of enums:

enum enums_name {
    .......
}

Syntax: Following is the class declaration

class class_name {
    constructor () {
        .....
    }
    // Methods and variables.....
}

Following are the examples which show enums and class output on execution over the console.

Example 1: In this example, we will simply create enum and class and their randomly passed results.

Javascript




enum fruits_name {
  APPLE = 'Apple',
  MANGO = 'Mango',
  GRAPES = 'Grapes'
}
  
console.log('Enums Output : ');
console.log(fruits_name);
class Person {
  name : string;
  
  constructor (name : string) {
    this.name = name;
  }
  
  displayName () : void {
    console.log(`Name of a Person is : ${  this.name}`);
  }
}
  
const person = new Person('neveropen');
person.displayName();


Output:

Enums Output : 
{ APPLE: 'Apple', MANGO: 'Mango', GRAPES: 'Grapes' }
Name of a Person is : neveropen

Now that we have understood in detail about enums and classes let us dive into our task of creating a basic Salary Calculator through the following example.

Example 2: In this example, we will create a basic salary calculator, in which the user will pass a total salary amount and further our logic will separate Basic Salary, Medical amount, House Rent amount, and Conveyance amount and displays the result over the console.

Javascript




enum SalaryDivision {
  
    // Following values could be considered in %
    Basic = 50,
    HouseRent = 30,
    Medical = 10,
    Conveyance = 10
}
  
class SalaryCalculator {
    GrossSalary : number;
      
    constructor(GrossSalary : number) {
        this.GrossSalary = GrossSalary;
    }
      
    displaySalary() : void {
        let BasicSalary : number = (this.GrossSalary 
            * SalaryDivision.Basic) / 100;
        let HouseRent : number = (this.GrossSalary 
            * SalaryDivision.HouseRent) / 100;
        let Medical : number = (this.GrossSalary 
            * SalaryDivision.Medical) / 100;
        let Conveyance : number = (this.GrossSalary 
            * SalaryDivision.Conveyance) / 100;
        console.log("Basic Salary : " + BasicSalary);
        console.log("HouseRent : " + HouseRent);
        console.log("Medical : " + Medical);
        console.log("Conveyance : " + Conveyance);
    }
}
  
let salary1 = new SalaryCalculator(1000000);
let salary2 = new SalaryCalculator(50000000);
  
salary1.displaySalary();
salary2.displaySalary();


Output:

Basic Salary : 500000
HouseRent : 300000
Medical : 100000
Conveyance : 100000
Basic Salary : 25000000
HouseRent : 15000000
Medical : 5000000
Conveyance : 5000000

Reference: https://www.typescriptlang.org/docs/handbook/enums.html

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS