You've probably heard it before: "Now is a great time to buy stocks." It's true that valuations are low, and bargains have multiplied. Still, you might be worried about following this advice. Rising inflation and general economic woes have weighed on companies' earnings in recent times. And to make matters worse, the Federal Reserve is predicting a recession later this year.

So, you may not be eager to jump into the market. Still, it's important to remember long-term investing is the sort of investing that generally brings great returns. And when you invest for long periods of time, you're sure to encounter a recession. Now the big question is: Should you really buy stocks before a recession -- or should you wait? Let's find out.

What history says

History shows us indexes often decline during a recession. This was the case during the Great Recession of 2007 through 2009 and the previous recession in the early 2000s. But these periods are pretty short in the context of long-term investing. If you're investing for the long term, count on holding a stock for at least five years.

Now, let's look at the indexes' performance over a period of seven years. The period starts several months ahead of the Great Recession and ends five years later. As we can see in the chart below, putting money into the stock market before the recession would have been a wise idea. Yes, indexes slipped during the worst of times, but they went on to post enormous gains.

^SPX Chart

^SPX data by YCharts.

Some people might argue that you would have benefited even more if you bought during the recession, at the very lowest point. But there are two things to keep in mind here. First, it's impossible to time the market and buy and sell at the most advantageous times. So, you're better off buying a stock when you think the price is reasonable and selling when you feel a stock has reached its maximum potential.

Second, there's no guarantee an index or individual stock will reach its lowest during a recession. It may do that prior to the recession. Again, we're back to the above point: We shouldn't waste our time trying to time the market.

As I'm sure you've guessed by now, I'm clearly making a case for buying stocks ahead of a recession. Now here's another point in favor of investing ahead of a recession. Earlier, I briefly mentioned the idea of low valuations being a reason to invest. Today, ahead of the "mild" recession recently predicted by the Fed, valuations of many stocks are historically low.

Teladoc and Carnival

For example, telemedicine giant Teladoc Health (TDOC -2.91%) is trading at its lowest ever in relation to sales. And cruise operator Carnival (CCL 1.13%) (CUK 0.88%) is also trading at around its lowest by that measure. These companies' earnings reports show progress along the path to recovery. So now may be the right time to buy.

You'll find bargains on stocks across industries. That's because, in these difficult economic environments, some investors do flee, and this opens up the door of opportunity for those who stay calm and focus on companies' long-term prospects.

Now, you might wonder about what sorts of stocks to buy ahead of a recession. This depends on your investment style. If you're cautious, you may look to the safety of healthcare stocks and companies that pay dividends. If you're an aggressive investor, you may go for the beaten-down growth players that have what it takes to excel once the economy improves.

In either case, though, if you focus on the long term and buy companies with overall earnings strength and bright future prospects, you're likely to set yourself up for a win. And that's something you'll want to do as soon as possible whether a recession is on the horizon or not.