Home >Web Front-end >JS Tutorial >How to calculate and print bonus and total using base salary via JavaScript?
We first multiply the base salary by a certain percentage to determine the bonus ratio. Next, we multiply the bonus percentage by the base salary to calculate the bonus amount. Finally, we add the bonus amount to the base salary to determine the total salary and print all three values.
Here is an example of how to calculate and print bonus and total salary using base 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);
The first line declares a variable basicSalary and assigns it a value of 5000.
The second line uses the * operator to calculate the bonus by multiplying the value of basicSalary by 0.1 (10% of base salary).
The third line calculates the total salary by adding the values of basicSalary and bonus using the operator.
The next few lines use the console.log() method to print the values of basicSalary, bonus and grossSalary.
NOTE - The above code snippet is a minimal working example, in a real-life scenario you should use validation, error handling and user input to get salary, bonus percentage and other relevant details. p>
The above is the detailed content of How to calculate and print bonus and total using base salary via JavaScript?. For more information, please follow other related articles on the PHP Chinese website!