As inflation continues to soar, many Americans are concerned that a recession could be coming. The Fed has hiked interest rates three times so far in 2022 in an effort to curb inflation. But increased interest rates can also slow consumer spending, which could potentially lead to a recession.

To be clear, nobody can say for certain whether it will happen or not. But if it does, should you still be investing in the stock market right now? Here's what you need to know.

How safe is the stock market now?

While recessions are not easy to stomach, they can actually be smart opportunities to invest. Market downturns often go hand in hand with them, and right now stock prices are lower than they've been in months.

That might not sound like good news, but it does mean that it's a more affordable time to invest. Even the most expensive stocks are heavily discounted, with some of them having fallen 30%, 50% -- even 70% or more during this market slump.

Buying during a recession not only allows you to invest on the cheap, but it also sets you up to see significant gains when the market eventually recovers.

Consider the Great Recession, for instance. When stock prices hit rock bottom in March 2009, it was discouraging for many investors. But over the next year alone, the S&P 500 saw returns of close to 70%. If you had invested when stock prices were at or near their lowest, you would have seen significant earnings the following year.

^SPX Chart

^SPX data by YCharts.

It's not easy to invest when the market is in a slump, and if we face a recession, stock prices could drop even further. But by taking advantage of the lower prices, you could be in a fantastic position when the market recovers.

When you shouldn't invest

While recessions can be smart investing opportunities, there are a couple of situations where you're better off avoiding the stock market.

If you don't have an emergency fund, for example, it's wise to focus on that goal first. If you lose your job or face an unexpected expense, it's a good idea to have some savings to fall back on.

Also, avoid investing any money you expect to need in the next few months or years. If the market falls further, the last thing you want is to withdraw your money when stock prices are at their lowest, selling your investments at a steep discount.

If now is not the right time to invest, that's OK. Focus first on strengthening your finances and building a solid safety net. If you have any cash left over, you can consider investing it then.

Recessions aren't easy, and nobody knows for certain whether a full-blown version will strike. But if you can afford it, continuing to invest is one of the best moves you can make to generate long-term wealth.