At the time of this writing, the S&P 500 (^GSPC -0.29%) is less than half a percentage point off its all-time high. With a 9.7% year-to-date gain, the index is on track to have another solid year, despite a brutal sell-off earlier this spring.

The S&P 500 is now up over threefold in the last decade and more than 50% in the last three years -- which may make some investors hesitant to buy stocks at all-time highs. Here's the best way to approach investing in the stock market at elevated levels, including a major mistake worth avoiding.

Light beams emitting from electronic components.

Image source: Getty Images.

Stocks aren't like consumer goods

One of the most common mistakes when investing in a stock after a major run-up is assuming it's expensive, just because its price is higher.

Stocks represent partial ownership in businesses and aren't commodities or household goods. If the cost of a Toyota Camry suddenly jumped to double that of the Honda Accord, then Camry sales would likely plummet, as many buyers would view the Camry as overpriced.

Stocks don't work like that. They aren't products with set specifications and design elements, but evolve as the business evolves. Sometimes, a business can reinvent itself, leading its stock price to surge for all the right reasons. Two examples of this dynamic hiding in plain sight are Nvidia (NVDA -0.85%) and Microsoft (MSFT -0.40%).

Transformational growth

Nvidia was founded in the early 1990s to create 3D graphics for the gaming and multimedia markets. Recently, the company's data center segment has surpassed its gaming and professional visualization segment.

In Nvidia's most recent quarter -- the first quarter fiscal 2026 -- data center revenue made up 88.7% of total revenue. The company may have started as a gaming company, but it's something entirely different today.

Valuing Nvidia based on what it was rather than where it's going would have made the stock look expensive at any point over the last few years. So far, Nvidia has backed up the torrid ascent of its stock price with earnings growth, and there's reason to believe that can continue.

Microsoft hasn't undergone as dramatic a transformation as Nvidia. The core applications that make up the Microsoft 365 software suite are nothing new, and Microsoft's cloud computing platform Azure has been a major player in the space for over a decade. However, the company's revenue growth rate and profit margin are on another level -- which is driving its surging stock price.

Microsoft is achieving these incredible results largely due to growing demand for Azure and the company's integration of artificial intelligence (AI) across its business, from legacy software to cloud, Azure OpenAI, gaming, electronics, and more. It has leveraged AI to enhance its products and services, and that's driving margin growth.

Like Nvidia, Microsoft is a good example of a stock at an all-time high because the underlying business is doing well and is expected to continue doing well.

Balancing quality and valuations

When a major index like the S&P 500 is near an all-time high and coming off an extended period of outsized gains, the market is viewed as expensive. However, it doesn't mean that all stocks are overvalued.

There are plenty of S&P 500 components at multiyear lows, and there are many that may have run up too far, too fast, to where their valuations are getting separated from fundamentals. The key is to stick with the companies that can justify their valuations because they have proven business models, impeccable growth prospects, or a combination of the two.

Buying stocks like Nvidia and Microsoft after their massive gains isn't a bad idea, but only if you believe the results are here to stay and can be built upon.

Nvidia and Microsoft make up a combined 14.6% of the S&P 500. Throw in the rest of the "Ten Titans" -- Apple, Amazon, Alphabet, Meta Platforms, Broadcom, Tesla, Oracle, and Netflix -- and that's 39% of the index. Investing in the S&P 500 at an all-time high inherently means that you believe the companies leading the index are still good buys.

The higher the valuation of a stock or exchange-traded fund (ETF), the more conviction you need to justify paying a premium. If there's a terrific company on sale, you don't need that much to go right to make a solid investment case.