After a tumultuous couple of years, the stock market has been surging recently. So far this year, the Dow Jones Industrial Average (^DJI 0.40%) is up by around 12%, the S&P 500 (^GSPC 1.02%) has risen by nearly 23%, and the Nasdaq (^IXIC 2.02%) has soared by a whopping 41%.

While many investors are excited about the possibility of a new bull market, others are worried that this is only a temporary rally and that prices will fall again.

It can be tough, then, to decide whether to invest now or wait. If prices are on their way up, now could be a fantastic time to buy. But if you invest now and the market dips, your portfolio might immediately lose value. The good news, though, is that your timing may not matter as much as you think.

Person sitting at a desk looking at a laptop.

Image source: Getty Images.

Is it safe to invest right now?

It can be tempting to wait until the perfect time to invest in the stock market. After all, if you invest when prices are at their lowest and then sell when they peak, you could potentially make a hefty profit.

However, even the experts can't predict what the market will do in the short term. Case in point: For most of this year, economists have been predicting everything from a stock market crash to a 2008-level recession. Few would have imagined that major market indexes would be reaching new highs by the end of the year.

If you're waiting for the best time to buy, then, you may end up waiting forever -- and missing out on precious time to build wealth in the meantime.

A better option, then, is to simply invest consistently no matter what the market is doing. This strategy is called dollar-cost averaging, and it involves buying at regular intervals throughout the year. Sometimes you'll invest when prices are at their peaks, and other times you'll buy at a discount. Over time, those highs and lows should average out.

What if stock prices fall again?

It's nerve-wracking to invest in times like these, especially if the market ends up taking a turn for the worse. But if you're able to keep a long-term outlook, it can make investing a little easier regardless of what happens.

The market will always be volatile in the short term. But over decades, it has consistently earned positive total returns despite volatility. Over the past two decades alone, for instance, the market has faced the dot-com bubble burst, the Great Recession, the COVID-19 crash, and the current slump. Yet the S&P 500 is still up by nearly 221% since 2000.

^SPX Chart

^SPX data by YCharts

Even if you invest at a "bad" time, you could still earn more over the long haul than if you were to wait for a better time to buy.

For example, say you had invested in an S&P 500 index fund in February 2009. This was immediately before the index bottomed out amid the Great Recession, and it may have seemed like the worst possible time to invest. But by the end of the year, you'd still have earned returns of more than 35%.

^SPX Chart

^SPX data by YCharts

On the other hand, say you had waited until September of that year to buy. The market was already surging by then, and it may have seemed like a safer time to invest. However, you'd have only earned returns of around 12% by the end of the year.

In other words, it's often better to invest now and ride out any potential storms than to wait. The market could still fall again in the coming weeks or months. But over several years, it's extremely likely to rebound. By investing now, you're more likely to earn more over time -- regardless of what happens in the near term.