With the markets doing well, and the S&P 500 and Nasdaq Composite reaching new heights, it is difficult to think about these halcyon days ending. But we know they do come to an end, and it is often abrupt.

Oftentimes, it is an unexpected event, such as the onset of the COVID-19 pandemic last year. However, that doesn't mean you can't prepare and prosper.

Hopefully, you have set up an emergency fund to cover several months' worth of expenses, and have taken other prudent steps. That's because this puts you in the enviable position to buy shares in companies that do well in various economic environments, but particularly when things get tough. When investors swiftly sell, good stocks go down too. That means you can likely pick up shares in these companies at bargain levels.

A man pointing out a declining chart to several people.

Image source: Getty Images.

1. Dollar General

Dollar General (NYSE:DG) is a retailer that prices most of its merchandise under $10. More than three-quarters of its sales come from its consumables category, which consists of basic, everyday items like paper towels, toilet paper, milk, eggs, and soap.

The low prices on everyday items appeal to consumers, particularly when they either lose their job or feel their position is in jeopardy. Last year, with many people forced to stay home due to the pandemic, Dollar General's same-store sales (comps) jumped 16.3%. Proving it doesn't merely thrive in tough times, this made it 31 straight years with a comps increase.

It doesn't just grow sales, either. Dollar General has also increased its profitability. Last year's operating income was $3.6 billion, 54% higher than the year-ago period.

Although you can find higher dividend yields than Dollar General's 0.8%, the board of directors has reliably raised the payment annually since its first payout in 2015. This includes increasing April's dividend by 17% to $0.42. The combination of growing profitability and continued dividends during challenging days is particularly appealing.

2. Walmart

Walmart (NYSE:WMT) is the world's largest retailer. This came about by its obsessive focus on keeping costs down to pass these savings on to customers. In fact, it has become a popular shopping destination since it is difficult to undercut Walmart on price.

Aside from that, management began pushing digital initiatives more than 20 years ago when it created e-commerce websites. Since then, it has moved ahead with omnichannel programs to compete with online retailers. This includes launching the Walmart+ subscription service last fall, providing subscribers with discounts, fast delivery, and a quicker checkout process at the stores.

It's good that the company is not standing still while others, particularly Amazon (NASDAQ:AMZN), continue to become more popular. Management constantly moves ahead, investing heavily this year in areas such as supply chain and efficiency initiatives.

As last year demonstrates, Walmart's low prices, always popular, become particularly relevant when the going gets tough. Its adjusted revenue rose by 7.7% to $564.2 billion, helping drive operating income 9.3% higher.

Plus, the company has raised dividends annually since its initial payment in 1974, making it a Dividend Aristocrat. Walmart's dividend yield is 1.6%.

Remember, markets crash for a reason, usually an unexpected and unpleasant surprise that wreaks havoc on the economy. But while other companies may struggle, it is good to know that you can buy these two stocks knowing they should draw more customers due to their low prices.

That will leave you feeling better even if the outside world doesn't feel great at the time.

This article represents the opinion of the writer, who may disagree with the “official” recommendation position of a Motley Fool premium advisory service. We’re motley! Questioning an investing thesis -- even one of our own -- helps us all think critically about investing and make decisions that help us become smarter, happier, and richer.