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 −

// 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);

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.

Note − The above code snippet is a minimal working example, in real world scenario, you should use validations, error handling, and user input for salary, bonus percentage and other relevant details.

Output

Updated on: 16-Feb-2023

663 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements