Now that the moratorium on student loans is set to end later this year, millions of borrowers will need to start factoring those payments into their budgets.

This will be especially challenging as the cost of living continues to increase, and many people don't have much cash to spare right now.

If you're already investing in the stock market, you may be wondering whether it's worthwhile to continue working toward that goal or to focus on paying off your student loans. Here's how to decide which goal you're better off pursuing right now.

Person looking at a computer feeling confused.

Image source: Getty Images.

Invest in the stock market or pay off debt?

First and most importantly, ensure you're at least making the minimum payments on your student loans. Skipping payments will not only make it harder to pay off your debt later on, but it can also affect your credit score and overall financial health.

From there, your options are to put any spare cash toward paying down debt or invest.

To decide which option is best right now, determine how the interest rate on your loans compares to the average rate of return you could be earning on your investments. Whichever one is higher is the financial goal you may want to pursue.

For example, say you could hypothetically earn an 8% average annual return on your investments, while your student loans carry a 5% annual interest rate. In this case, you'd earn more investing than you'd save by paying off your debt.

On the other hand, if you have high-interest debt and are paying more in interest than you'd earn by investing, you may be better off focusing on paying down your loans right now.

Investing when the market is volatile

The current market volatility (and potential upcoming recession) could throw a wrench in your plans. Most people have watched their investments plummet over the past year, and it may not seem wise to invest at all right now.

However, it's important to remember that investing is a long-term goal. You may not see substantial returns right now, but the market's long-term potential is more important than the short-term ups and downs.

^SPX Chart

^SPX data by YCharts

In other words, don't let the current turbulence scare you away from investing. The stock market as a whole has historically earned an average rate of return of around 10% per year over decades, despite experiencing extreme volatility in that time. Sticking it out for the long term means this downturn will only be a blip on your radar.

One more caveat to consider

As if two important financial goals aren't enough, there's a third one you may want to think about: building an emergency fund.

An emergency fund is critical during periods of market volatility, because it can help protect your savings in the event of a recession. If you lose your job or face an unexpected expense and have no savings, you may have no choice but to pull money from your investments.

However, stock prices often fall during a recession. If you need to withdraw your money when the market is down, you could end up selling your investments for less than you paid for them and locking in those losses.

If you have no emergency savings at all, this may be the most important goal to focus on first (after making the minimum payments on your loans). Once you have a few months' worth of savings built up, you can decide whether to invest or start paying down your debt.

Times are tough right now for millions of Americans, especially those saddled with student loan debt. But by prioritizing your goals carefully, you can make the most of every dollar.