Amazon.com Inc. justified its big investments to keep operating through the COVID-19 pandemic with sales growth and a record profit that far exceeded analysts’ estimates, showing that staying open when so many businesses were forced to close was a rare opportunity.

The online retail giant spent more than $4 billion (¥418.2 billion) in the second quarter to clean warehouses, hire employees and entice them back to work with temporary pay raises while much of the country shut down. That push paid off as customers shifted from buying groceries and emergency supplies early in the pandemic to bigger orders with electronics and housewares to settle in at home for the long haul.

Second-quarter revenue jumped 40 percent from a year earlier to $88.9 billion. Earnings were $10.30 a share, beating analysts’ average projection of $1.51 per share on sales of $81.2 billion, according to data compiled by Bloomberg.