The S&P 500 ended higher on Thursday after a day of choppy trading, as gains in defensive and tech shares countered declines for economically sensitive groups as worries persisted about a potential recession.

The S&P 500 swung between positive and negative during the session, as investors weighed whether the Federal Reserve's aggressive rate hikes to control surging inflation would wound the economy.

Benchmark U.S. Treasury yields fell to two-week lows, supporting tech and other growth stocks.

Trading has remained volatile in the wake of the S&P 500 last week logging its biggest weekly percentage drop since March 2020. Investors are weighing how far stocks could fall after the index earlier this month fell over 20% from its January all-time high, confirming the common definition of a bear market.

"There is a tremendous amount of uncertainty about the outlook and so the market is confused," said Walter Todd, chief investment officer at Greenwood Capital in South Carolina.

According to preliminary data, the S&P 500 gained 36.17 points, or 0.96%, to end at 3,796.06 points, while the Nasdaq Composite gained 180.02 points, or 1.63%, to 11,233.10. The Dow Jones Industrial Average rose 200.60 points, or 0.66%, to 30,683.73.

In his second day of testifying before Congress, U.S. central bank chief Jerome Powell said the Fed's commitment to reining in 40-year-high inflation is "unconditional" but also comes with the risk of higher unemployment.

U.S. business activity slowed considerably in June as high inflation and declining consumer confidence dampened demand across the board, a survey on Thursday showed.

"The Fed wants to see things start to slow and the data is starting to reflect that," said James Ragan, director of wealth management research at D.A. Davidson.

Citigroup analysts are forecasting a near 50% probability of a global recession.

"Economic growth is slowing. Is it going to slow enough to go into a recession, that's the big question," Ragan said.

Defensive groups considered safer bets in rocky economic times were among the top-performing S&P 500 sectors, including utilities, consumer staples and healthcare.

Gains in tech heavyweights Microsoft and Apple also helped support the S&P 500.

The energy sector slumped, continuing its recent pullback after soundly outperforming the market for most of 2022. Declines in Exxon Mobil and Chevron were among the biggest individual drags on the S&P 500.

Other economically sensitive sectors also fell, including declines for materials and financials.