If you end up with a little extra money in your bank account — from a tax refund, a bonus, or from living below your means — you may wonder what you should do with this cash. Should you put it toward your student loan or invest it?

This decision depends on several factors, like your interest rates, tax rate and personal preference. Let’s break this down with a general rule you can use as well as some things you’ll want to consider when making this decision for yourself.

A General Rule of Thumb

The decision often boils down to math: where will you get the best return?

If you are deciding between paying off your student loans and investing you’ll need to compare:

  1. The after-tax interest rate you’re paying on your student loans, and
  2. The after-tax return you expect from your investments

If you expect to earn more on your investments than you’re paying in interest on your student loans, you’d put the extra money toward investing.

For example, say your student loan has a 7% interest rate. You currently pay $1,000 per year in interest and make $60,000 per year, which puts you in the 22% income tax bracket. Because you can deduct the amount of student loan interest you pay on your income tax return, up to $2,500 per year, the after-tax interest rate you’re paying on your student loan is 5.46%.

You can calculate that by multiplying the interest rate on your debt by (1 – your marginal income tax rate). In this example, it’s 7% x (1 – 22%) = 5.46%.

Once you know the after-tax rate you’re paying on your student loan debt, you need to find the after-tax return on your investments so you can compare the two.

If you’re planning to invest in the stock market, the S&P 500 has historically returned about 7%, adjusted for inflation. But since anything you earn on investments is taxable, you need to account for taxes that you’d pay. If you invest in the stock market and earn a 7% return, your after-tax return on investment would be 5.95%.

In this example, you’re planning to hold the investments long-term and your income is $60,000 per year. The tax rate you’d pay on the capital gains (your investment income) is 15%. To figure out your after-tax rate of return on your investment, you’d multiply 85% (the percentage of the gains you get to keep, or 100% – 15% = 85%) by 7% (the average rate of return on your investment). 85% * 7% = 5.95%.

In this example, it looks like a better idea to invest the money, rather than pay off your debt. But, there are additional factors that may affect the decision.

Key Considerations

Figuring out what’s best isn’t as simple as using a few formulas. There are a number of other things you’ll want to consider as you make this decision.

Take Advantage of Your Employer 401(k) Match

If your employer offers to match your retirement plan contributions, it’s generally considered to be a good financial move to prioritize that. Why? It’s free money. If you’re not currently putting away enough money to get the full match, use the extra money here.

For example, suppose your company offers to match $0.50 on each dollar that you contribute to your 401(k), up to a maximum of 6%. If you earn $50,000 annually and contribute $6,000 to your 401(k), your employer will contribute $3,000, the full match you can receive.

But let’s say you’re not contributing enough to take advantage of the employer’s full match. If you’re contributing only $1,000 to your 401(k), your employer will contribute $500. In this situation, you can earn an additional $2,500 by increasing your contribution by $5,000. That’s a pretty good return on investment!

Paying Off Debt Is Like a Guaranteed Return on Your Investment

Suppose you have $5,000 and you’re considering paying down your student loan, which has a 6% fixed interest rate. When you pay off the debt you gain a guaranteed return of 6% because of the interest you are no longer paying. For some, knowing that they are paying off debt and getting that guaranteed return is a big incentive because it helps them feel more comfortable financially.

Paying Off Low-Interest Debt Likely Isn’t a Good Return on Investment

If you have a very low fixed interest rate on your student loan, such as 3%, you may prefer to invest the money instead of paying off your student loan debt. With a fixed interest rate that low, you can almost certainly find a low-risk investment that pays a higher after-tax return on investment. Without needing to go into calculations, you know that over the long run, most investments will be a better bet.

That’s why Warren Buffet famously kept a mortgage on a vacation home, despite his wealth, because the interest rate on his mortgage was low.

Stock Market Returns Aren’t Guaranteed.

On average, the S&P 500 has an annualized return of about 7%, adjusted for inflation. But, you shouldn’t expect a 7% gain each year. The stock market can be volatile. There will be some years when the stock market returns are negative, meaning you’ve lost money on your investment. For example, the S&P 500 lost almost 40% in 2008. While investing in the stock market is risky, most experts agree that it’s still a good long-term bet.

Nevertheless, paying off student loan debt provides a risk-free return on investment, while investing in the stock market carries some risk. A simple comparison of the after-tax interest rates does not reflect the difference in risk.