U.S. stocks edged higher Tuesday, pushing the S&P 500 and the Nasdaq Composite to new records, after a choppy trading session that saw many major sectors swing between losses and gains.

The broad benchmark index ticked up 1.19 points, or less than 0.1%, to close at 4291.80, after wobbling between gains and losses for parts of the afternoon. The gain marked the S&P 500’s 33rd record close of the year—a figure that ties the number seen in all of 2020.

The Nasdaq Composite also rallied Tuesday, rising 27.83 points, or 0.2%, to 14528.33, marking the technology-heavy index’s 19th record close of the year. The Dow Jones Industrial Average, meanwhile, added 9.02 points, or less than 0.1%, to finish at 34292.29. The index of blue-chip stocks remains 1.4% away from its last record closing high reached in early May.

All three major indexes finished Tuesday off their session highs. Earlier in the day, economically sensitive stocks rallied after private research group The Conference Board said its index of consumer confidence rose in June, beating analysts’ expectations. The survey, which offers insight into Americans’ willingness to spend on goods and services, found that consumers are optimistic about business conditions and their own financial prospects in the months ahead.

Additional housing data released Tuesday also added to that sentiment, with new data showing that U.S. home prices surged at their fastest pace ever in April. Shares of home builders including PulteGroup and Lennar finished the day higher, rising 2% and 0.8%, respectively.

Stocks have been rising in recent weeks, propelled by investors’ confidence in the economic recovery and the prospect of fresh government spending.

Yet money managers also point to some signs of anxiety under the surface. Many investors remain cautious about whether the more-transmissible Delta variant of the coronavirus could slow the global economic recovery or disrupt supply chains for crucial goods.

Inflation also remains a top concern for investors at the moment. A Bank of America Global Research survey from June found fund managers currently view both inflation and a bond market “taper tantrum”—meaning a possible spike in Treasury yields once the Fed indicates it will tighten monetary policy—as the top tail risks for the markets.

“You’re seeing the tug of war between value versus growth right now, and where it’s really coming from is the tug of war between whether inflation is transitory or real,” said JJ Kinahan, chief market strategist at TD Ameritrade.

Many market strategists are anticipating choppiness to continue—especially during a week that is expected to bring muted volumes ahead of the Fourth of July weekend. Many are also awaiting Friday’s jobs numbers. Economists are expecting 706,000 new jobs to be added to the U.S. economy, according to a survey compiled by The Wall Street Journal.

Still, some analysts said they expect stocks to grind higher.

“We have further to run when it comes to equities, but I think there is some form of summer lull coming and it could be that we see some zigzagging,” said Daniel Egger, chief investment officer at St. Gotthard Fund Management, about small potential market peaks and valleys.

In corporate news, Morgan Stanley rose $2.94, or about 3.4%, to close at $90.64. The bank on Monday doubled its quarterly dividend and said it plans to buy back up to $12 billion of its stock. Goldman Sachs gained $3.91, or 1.1%, to finish at $372.62 after raising its dividend.

Many technology companies also rallied, with social-media company Snap gaining $1.35, or 2%, to close at $67.57. Chip maker Advanced Micro Devices rose $2.44, or 2.8%, to $89.52.

Brent crude, the international oil benchmark, rose 0.1% to $74.76 a barrel. The Organization of the Petroleum Exporting Countries and its allies are meeting Thursday, where they are expected to discuss a modest increase in production.

In bond markets, the yield on the benchmark 10-year U.S. Treasury note edged up to 1.479%, from 1.478%. Yields rise when bond prices fall.

Overseas, the Stoxx Europe 600 rose 0.3%.

Japan’s Nikkei 225 fell 0.9% by the close of trading, while the Hang Seng Index dropped 0.9%. In mainland China, the Shanghai Composite Index fell 0.9%.

A trader stood outside of the New York Stock Exchange on Monday.

A trader stood outside of the New York Stock Exchange on Monday.

Photo: andrew kelly/Reuters

Write to Will Horner at William.Horner@wsj.com and Caitlin McCabe at caitlin.mccabe@wsj.com