A strong grocery business helped Walmart's online sales grow 41% in the third quarter, the company said Thursday, fueling an earnings beat and 21 quarters of growth in the U.S.

Total sales in the period fell short of analysts' expectations, but Walmart raised its annual earnings outlook — for the second time this year — ahead of the holiday season.

Walmart shares were recently up about 1%, having climbed more than 30% this year. During trading Thursday morning, the stock hit a 52-week high of $125.38.

Walmart is "prepared for a good holiday season," CEO Doug McMillon said in the earnings statement.

Here's what Walmart reported for its fiscal third quarter compared with what analysts were expecting, based on Refinitiv data:

Earnings per share: $1.16, adjusted, vs. $1.09 expected

Revenue: $127.99 billion vs. $128.65 billion expected

U.S. same-store sales: up 3.2% vs. growth of 3.1% expected

Net income for the period ended Oct. 31 grew to $3.29 billion, or $1.15 a share, compared with $1.71 billion, or 58 cents per share, a year ago. Excluding one-time charges, Walmart earned $1.16 per share, topping expectations for $1.09 in a Refinitiv survey of analysts.

Programming note: Walmart CEO Doug McMillon will address CNBC's Evolve event next week. Register to attend at cnbcevents.com/evolve.

Total revenue grew 2.5% to $127.99 billion from $124.89 billion a year ago. But that fell slightly short of expectations for $128.65 billion.

Sales online and at Walmart stores operating for at least 12 months in the U.S. were up 3.2%, topping estimates for growth of 3.1%.

Walmart said the average ticket in the U.S. grew 1.9% compared with a 1.8% increase a year ago. Transactions were up 1.3% in the latest quarter, slightly lower than growth of 1.6% this time last year.

Strong growth in grocery

The company said e-commerce sales were up 41%, thanks to "strong growth" in online grocery. The company has been calling for U.S. e-commerce sales growth of 35% for the year, which would be slightly less than what it logged in fiscal 2019.

"We continue to see good traffic in our stores," McMillon said. "We're growing market share in key food and consumables categories, including fresh."

"We need to translate this repetitive food and consumable volume into a stronger Walmart.com business that's profitable over time, so that's what we're working on," the CEO added.

Walmart's digital operations have come under fire, with tension brewing internally, as the company still loses money online for all the investments it has made to acquire brands and speed delivery. Walmart said its operating income during the third quarter fell 5.4% from a year earlier.

All year long, Walmart and Amazon have been neck and neck in a delivery war, trying to offer shoppers as speedy a service as possible.

Amazon announced plans in October to start delivering grocery products for free within a two-hour window to all Prime members living in the 2,000 regions eligible for the service. Until then, Prime members had to pay an additional $14.99 per month to get access to Amazon Fresh, a separate program that offered two-hour grocery delivery.

Walmart has started testing delivering groceries directly to customers' refrigerators in three cities. Its InHome grocery delivery membership program costs $19.95 a month. And making the most of its bricks-and-mortar stores, it has more than 2,700 grocery pickup locations for online orders across the United States. Walmart also now offers a "Delivery Unlimited" option from 1,400 locations, where customers can pay $98 annually, or $12.95 monthly, for unlimited grocery delivery.