The Nasdaq Composite and the S&P 500 hit record highs as strong earnings and healthy growth in gross domestic product buoyed stocks across the board.

Twitter Inc., Coca-Cola Co. and McDonald’s Corp. rose Friday after upbeat earnings reports this week.

Google’s parent, Alphabet Inc., helped lead stocks higher with a banner day, surging after...