U.S. stocks rallied for a fourth day on Thursday after JPMorgan's robust results added to growing optimism about the quarterly earnings season and technology shares rose in anticipation of more good news.

Gains in International Business Machines Corp helped the market shake off its earlier malaise. The broad S&P 500 extended the week's gains to tally its best four days since March as stocks recovered from 12-year lows.

IBM rose 3.2 percent to $110.64 and provided the Dow's biggest boost during the regular session. The stock climbed 2.9 percent in extended trade after it released its quarterly results.

As the market rallies, implicit in that is the expectation that we'll continue to see a recovery in corporate profits, said Dean Curnutt, president of Macro Risk Advisors in New York.

JPMorgan Chase & Co continued the week's streak of positive results, posting a strong quarterly profit, but warning of deteriorating consumer credit. The bank's stock was kept under water for most of the day, edging down 0.4 percent to $36.13 on the New York Stock Exchange.

Better-than-expected results from Goldman Sachs and Intel set the tone early in the week amid expectations of a lousy second quarter marked by falling revenues and cost cuts.

The Dow Jones industrial average <.DJI> rose 95.61 points, or 1.11 percent, to 8,711.82. The Standard & Poor's 500 Index <.SPX> gained 8.06 points, or 0.86 percent, to 940.74. The Nasdaq Composite Index <.IXIC> advanced 22.13 points, or 1.19 percent, to 1,885.03.

Comments from leading economist Nouriel Roubini further boosted sentiment after he said the worst is past in terms of economic and financial conditions.

Shares of Qualcomm Inc lifted the Nasdaq after RBC Capital Markets started coverage of the cellphone chip supplier with an outperform rating. Qualcomm gained 1.4 percent to $46.72.

Google recovered from earlier losses to gain 1 percent to $442.60 on Nasdaq, but the stock fell 2.4 percent in extended trade shortly after its quarterly results came out.

On the economic front, government data showed the number of U.S. workers filing new claims for jobless benefits fell last week to their lowest level since January, but the seasonally adjusted data was amplified by earlier auto industry plant shutdowns.

A separate report showed factory activity in the U.S. Mid-Atlantic region shrank for the 10th consecutive month in July, a worse-than-expected decline that raised questions about the speed of the economic recovery.

(Editing by Jan Paschal)