DEV Community

Muhammad Ahmed
Muhammad Ahmed

Posted on

Calculating Overtime in JavaScript: Get the Regular Rate Right

Overtime calculation in JavaScript looks like a ten-minute job. Multiply the extra hours by 1.5 and you're done, right? It works until a bonus or a shift differential shows up, and then the numbers drift.

Here's a small, testable way to do it properly, with the regular rate handled the way US payroll expects.

The rule in one paragraph

Under the US Fair Labor Standards Act (FLSA), covered non-exempt employees get at least 1.5 times their regular rate for every hour over 40 in a workweek. The regular rate is the total straight-time pay for the week divided by the total hours worked. That's the catch: it isn't always the hourly wage.

Step 1: the naive version

This is what most people write first:

javascript
function naiveOvertime(rate, hours) {
const regular = Math.min(hours, 40) * rate;
const overtime = Math.max(hours - 40, 0) * rate * 1.5;
return regular + overtime;
}

console.log(naiveOvertime(24, 47)); // 1212

For a plain hourly wage, it's correct. A $24 rate and 47 hours gives $960 + $252 = $1,212.

Step 2: add the regular rate

Now let's say someone earns $22 an hour, works 45 hours, and gets a $120 production bonus that was promised in advance. That bonus is part of straight-time pay, so it raises the regular rate.

Because straight time has already been paid for every hour, you only owe the extra half on the overtime hours.

javascript
function overtimePay({ rate, hours, bonus = 0, multiplier = 1.5, threshold = 40 }) {
const otHours = Math.max(hours - threshold, 0);
const straightTime = rate * hours + bonus;
const regularRate = straightTime / hours;
const premium = otHours * regularRate * (multiplier - 1);

return {
regularRate: round(regularRate),
premium: round(premium),
total: round(straightTime + premium),
};
}

const round = (n) => Math.round(n * 100) / 100;

console.log(overtimePay({ rate: 22, hours: 45, bonus: 120 }));
// { regularRate: 24.67, premium: 61.67, total: 1171.67 }

Walk through the numbers: $22 × 45 = $990, plus the $120 bonus is $1,110. Divide by 45 hours and the regular rate is about $24.67. The half-time premium on 5 overtime hours is 5 × $24.67 × 0.5 = $61.67, so the total is $1,171.67.

Ignoring the bonus in the overtime math would give $1,165. That's $6.67 short. It's small on one check, but it repeats every pay period.

Why the multiplier is a parameter

Time and a half is the federal minimum, not a ceiling. Contracts and some local rules set higher premiums, and double time is common on certain holidays. Passing multiplier in keeps the function honest. The time and a half calculator uses the same idea: the multiplier is an input, and 1.5 is just the default.

Step 3: write tests before you trust it

Money code deserves tests. Three cases cover the edges:

javascript
import assert from "node:assert";

// Under 40 hours: no overtime
assert.equal(overtimePay({ rate: 20, hours: 38 }).total, 760);

// Exactly 40 hours: no overtime
assert.equal(overtimePay({ rate: 20, hours: 40 }).total, 800);

// Over 40 with a bonus
assert.equal(overtimePay({ rate: 20, hours: 44, bonus: 100 }).total, 1024.55);

The last case is worth checking by hand. Straight time is 44 × $20 + $100 = $980. The regular rate is $980 / 44 = $22.27. The premium is 4 × $22.27 × 0.5 = $44.55, which makes $1,024.55.

Step 4: two pay rates in one week

Some people work two roles at different rates. The federal method is a weighted average, so you add up all straight-time pay and divide by all hours.

javascript
function multiRateOvertime(shifts, threshold = 40) {
const hours = shifts.reduce((sum, s) => sum + s.hours, 0);
const straightTime = shifts.reduce((sum, s) => sum + s.hours * s.rate, 0);
const regularRate = straightTime / hours;
const premium = Math.max(hours - threshold, 0) * regularRate * 0.5;
return round(straightTime + premium);
}

console.log(multiRateOvertime([
{ hours: 30, rate: 18 },
{ hours: 18, rate: 24 },
])); // 1053

Check it by hand: (30 × $18) + (18 × $24) = $972 over 48 hours, so the regular rate is $20.25. The premium on 8 overtime hours is 8 × $20.25 × 0.5 = $81, and the total is $1,053. Your payroll system may use a different method if the employee and employer agreed to it in advance, so confirm before you rely on this.

Things the function doesn't handle

Keep this list in a comment so the next developer sees it:

Daily overtime. Some states, like California, count hours over 8 in a day. This function only looks at the week.
Pay-period boundaries. Each workweek must be calculated on its own before you add them up.
Exempt employees. Whether someone is exempt depends on duties and salary, not job title.
Rounding rules. Round at the end, not at every step.
Sanity-check against a calculator

When you're building anything payroll-related, compare your output with an independent source before it ships. A free tool like OT Pay Calculator is handy for this: enter a rate, hours, and multiplier, and compare the gross pay with what your function returns. If the figures differ, check the hour total first, then the rate, then the multiplier.

Wrapping up

Good overtime calculation in JavaScript comes down to three habits: compute the regular rate from total straight-time pay, treat the multiplier as an input, and test the boundary cases. Figures here are for learning and planning, not legal or tax advice, and rules vary by state.

What edge case has bitten you in payroll code? Share it in the comments.

Top comments (0)