The rebound in the stock market in the months since the COVID-19 pandemic started has been incredible. The S&P 500 is now 53% above its March lows and just hit a fresh all-time high as I sat down to write this. Think about that for a second: The stock market has completely shaken off the novel coronavirus for now.

And the moves in many individual stocks have been nothing short of remarkable, particularly when it comes to the technology sector. Apple (AAPL -2.15%) recently became the first U.S. company to reach a $2 trillion market cap, hitting $500 per share shortly before its stock is set to complete a 4-for-1 split. Many other popular tech stocks have doubled or more this year.

With all of that in mind, it is certainly understandable if stocks are starting to look a little expensive. However, if you're a long-term investor, that's not the right way to think about it.

Person in a beard, khakis, and a button-up shirt scratching their head with a confused expression on their face.

Image source: Getty Images.

Over the long run, price matters less and less

In the past 25 years, the stock market has had some amazing bull runs, as well as some downright scary crashes. The dot-com bubble (and bust), the 9/11 attacks, the housing bubble, the financial crisis, the longest bull market in history, and now the COVID-19 crash and rapid rebound have all taken place within the past quarter-century.

^SPX Chart

^SPX data by YCharts

So, over the short term, money that has been invested has produced quite a roller-coaster ride for investors. But it still has to be a terrible move to buy at the highs, right?

Consider this hypothetical scenario. The market's peak in 2007 -- before the financial crisis caused the S&P 500 to lose more than 55% of its value -- was arguably the worst possible time to put money to work in the stock market in the past few decades. From a short-term perspective, that's clearly true. A $10,000 investment in an S&P 500 index fund like the Vanguard S&P 500 ETF (VOO -0.28%) at the October 2007 peak would have declined to about $4,300 by the time the March 2009 lows were reached.

But how much do you think the investment would be worth today? You might be shocked to learn that a $10,000 investment in an S&P 500 index fund made at the worst possible time before the financial crisis would be worth nearly $29,000 as of August 2020. Sure, it would have been better to have bought closer to the lows. But the point is that even if you buy at the worst possible points, you'll likely do just fine over the long run.

If stocks look expensive, dollar-cost averaging could be the way to go

If you're worried that a certain stock or index fund might be too expensive, it could be a smart idea to use a strategy called dollar-cost averaging to buy shares.

In a nutshell, dollar-cost averaging involves investing fixed amounts of money in a stock at set intervals, as opposed to investing all of your money at once. For example, if you have $5,000 to invest in Apple, you might invest $1,000 each month for the next five months, instead of all $5,000 today.

Here's the idea. Let's say that you're right and Apple turns out to be way too expensive today. You invest $1,000 now and the stock proceeds to drop by 20% over the next month. Now you still have money to add shares at the new, cheaper price.

On the other hand, let's say you're wrong and Apple stays where it is, or even continues to spike higher. You've locked in some of your investment today at favorable prices, and if the stock gets more expensive, you'll end up buying fewer shares at the higher prices.

In short, dollar-cost averaging can be an effective way to get a mathematically favorable average cost for your stock positions, and can be especially useful for initiating new positions when stocks are looking expensive.

The Foolish bottom line

As mentioned, the S&P 500, Apple, and many other stocks (particularly in the technology sector) are at all-time highs. They could certainly pull back a bit from here, or they could keep climbing; nobody knows. But as long as you focus on building positions in high-quality stocks or low-cost index funds, and then hold onto them for a long time, it's faulty logic to think of the market in terms of "expensive" or "cheap."