How to calculate and print bonus and gross using basic salary by JavaScript?

We will first determine the bonus percentage by multiplying the basic salary by a certain percentage. Next, we will calculate the bonus amount by multiplying the bonus percentage with the basic salary. Finally, we will add the bonus amount to the basic salary to determine the gross salary and print all three values.

Here is an example of how to calculate and print the bonus and gross salary using the basic salary in JavaScript ?

Basic Example

// Declare basic salary variable
var basicSalary = 5000;

// Calculate bonus (10% of basic salary)
var bonus = basicSalary * 0.1;

// Calculate gross salary (basic salary + bonus)
var grossSalary = basicSalary + bonus;

// Print the values
console.log("Basic Salary: " + basicSalary);
console.log("Bonus: " + bonus);
console.log("Gross Salary: " + grossSalary);
Basic Salary: 5000
Bonus: 500
Gross Salary: 5500

Explanation

  • The first line declares a variable basicSalary and assigns it the value of 5000.

  • The second line calculates the bonus by multiplying the value of basicSalary by 0.1 (10% of basic salary) using the * operator.

  • The third line calculates the gross salary by adding the value of basicSalary and bonus using the + operator.

  • The next lines print the values of basicSalary, bonus and grossSalary using the console.log() method.

Using Function with Parameters

function calculateSalary(basic, bonusPercent) {
    var bonus = basic * (bonusPercent / 100);
    var gross = basic + bonus;
    
    console.log("Basic Salary: " + basic);
    console.log("Bonus (" + bonusPercent + "%): " + bonus);
    console.log("Gross Salary: " + gross);
    console.log("---");
}

// Test with different values
calculateSalary(6000, 15);
calculateSalary(8000, 20);
calculateSalary(4500, 12);
Basic Salary: 6000
Bonus (15%): 900
Gross Salary: 6900
---
Basic Salary: 8000
Bonus (20%): 1600
Gross Salary: 9600
---
Basic Salary: 4500
Bonus (12%): 540
Gross Salary: 5040
---

Advanced Example with Multiple Allowances

function calculateDetailedSalary(basic, bonusPercent, hra, da) {
    var bonus = basic * (bonusPercent / 100);
    var totalAllowances = hra + da;
    var gross = basic + bonus + totalAllowances;
    
    console.log("=== Salary Breakdown ===");
    console.log("Basic Salary: " + basic);
    console.log("Bonus (" + bonusPercent + "%): " + bonus);
    console.log("HRA: " + hra);
    console.log("DA: " + da);
    console.log("Gross Salary: " + gross);
}

// Calculate salary with HRA and DA
calculateDetailedSalary(10000, 15, 2000, 1500);
=== Salary Breakdown ===
Basic Salary: 10000
Bonus (15%): 1500
HRA: 2000
DA: 1500
Gross Salary: 15000

Note ? The above code snippets are minimal working examples. In real world scenarios, you should use validations, error handling, and user input for salary, bonus percentage and other relevant details.

Conclusion

Calculating salary components in JavaScript involves basic arithmetic operations. Use functions to make your code reusable and handle different salary structures efficiently.

Updated on: 2026-03-15T23:19:00+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements