Gains among technology companies helped break a three-day losing streak for US stocks on Friday although the market ended with its worst weekly loss since March.
The modest rebound came at the end of a turbulent week on Wall Street as escalating tensions between the US and North Korea rattled global markets.
In the first four days of the week, the Standard & Poor's 500 index swung from marking its latest record high to posting its biggest single-day drop in nearly three months.
The negative headlines provided many investors with an opportunity to pocket some of their recent gains following a string of record highs fuelled by strong corporate earnings.
"It's been a bit of a rollercoaster this week, with all the rhetoric between the US and North Korea," said Jeff Kravetz, regional investment strategist at US Bank Wealth Management.
"That did temporarily shake investors' complacency, but we think markets are ready to move higher in the back half of the year, and earnings and economic data are going to drive that."
On Friday, the S&P 500 rose 3.11 points, or 0.1%, to 2,441.32. The index had its biggest drop since mid-May a day earlier.
The Dow Jones industrials average gained 14.31 points, or 0.1%, to 21,858.32. The Nasdaq added 39.68 points, or 0.6%, to 6,256.56. The Russell 2000 index of smaller-company stocks picked up 1.69 points, or 0.1%, to 1,374.23.
The recovery fit a recent pattern of investors using dips to put more money in stocks.
Despite the past week's decline, the major indexes are in positive territory so far this year, led by the Nasdaq, which is up 16.2%. The S&P 500 is up 9%, while the Dow is up 10.6%.
"If you strip away what's going on in North Korea, and if you strip away what's going on in Washington, which are things that are tougher to predict, the economy, the global recovery, earnings, it all paints a very positive picture for the rest of the year," Mr Kravetz said.
Tensions between the US and North Korea continued to simmer early on Friday. In a tweet, President Donald Trump warned of military action "should North Korea act unwisely," noting that the US is "locked and loaded." Earlier in the week, Mr Trump said the US would unleash "fire and fury" on North Korea if it continued to threaten the U.S.
North Korea had announced a detailed plan to launch a salvo of ballistic missiles toward the US Pacific territory of Guam, a major military hub and home to US bombers.
Still, there were fewer signs of anxiousness in the markets on Friday.
Bond and gold prices, traditional havens for nervous investors, were little changed, and the VIX, a measure of how much volatility investors expect in stocks, fell 3.3% following a 44.4% jump the day before. It is still the highest it has been since May.