The last few years have been a wild ride for the stock market, and if you're feeling conflicted about investing right now, you're not alone.

Last month, researchers at Deutsche Bank made headlines for noting that they believe there's a "near 100%" chance the U.S. will face a recession in 2023. At the same time, though, major market indexes like the S&P 500 and Nasdaq have been surging, leading many to claim that we're in the early stages of a new bull market.

Warren Buffett at an event.

Image source: The Motley Fool.

All of this opposing information can make it a daunting time to invest. If a recession is around the corner, it may be tempting to stay out of the market for now. But if we really are in a new bull market, not investing now could be a missed opportunity.

Fortunately, legendary investor Warren Buffett has some wise words for times like these.

Should you invest in the stock market right now?

When the market is volatile and the economy feels unstable, it's easy to get caught up in the short-term ups and downs. But according to Buffett, the most intimidating periods are often the best times to invest.

Back in 2008, at the height of the Great Recession, Buffett wrote an opinion piece for The New York Times to help reassure nervous investors.

"A simple rule dictates my buying," he writes in the article. "Be fearful when others are greedy, and be greedy when others are fearful. And most certainly, fear is now widespread, gripping even seasoned investors."

Many stocks are still priced well below their peaks, meaning now is your chance to load up on high-quality investments at a discount. While investing when the future is uncertain can be intimidating, it can also set you up for significant long-term earnings.

What if a recession is still looming?

There's always a chance that the market could get worse before it gets better, especially if a recession is around the corner. But over many years, it's extremely likely that the market will see positive total returns despite short-term volatility.

For example, over the past two decades alone, the market has experienced everything from the dot-com bubble burst to the Great Recession to the COVID-19 crash and the current slump (along with countless smaller corrections along the way).

Despite everything, though, the S&P 500 is still up by more than 200% since 2000.

^SPX Chart

^SPX data by YCharts.

Even if more volatility is on the horizon in 2023, that doesn't mean now is a bad time to invest. It is, however, especially important to keep a long-term outlook. If you invest now and stock prices fall later this year, simply ride out the storm and wait for the market to rebound.

"I can't predict the short-term movements of the stock market," Buffett writes in the Times piece. "What is likely, however, is that the market will move higher, perhaps substantially so, well before either sentiment or the economy turns up. So if you wait for the robins, spring will be over."

The secret to surviving a downturn

The best way to set yourself up for success when investing is to ensure you're buying quality stocks from companies with healthy underlying fundamentals.

Not all stocks will be able to survive a potential recession. But the businesses with strong fundamentals -- like solid financials, a knowledgeable leadership team, and a competitive advantage -- will have the best chances. Put your money behind these types of stocks, and your portfolio will be far safer.

The last couple of years have been a roller coaster for the stock market. But if you're a long-term investor, these ups and downs shouldn't be too concerning. By investing in the right places and holding onto those stocks, you can set yourself up for potentially lucrative long-term gains.