New York City may be an expensive place to live. Jobs are not easy to find, even as the city rebounds from the recession. And the public transit system is not always reliable or comfortable.

But despite the challenges of city living, the city’s population is growing in ways not seen in decades.

For the third consecutive year, New York City last year gained more people than it lost through migration, reversing a trend that stretched to the mid-20th century.

For the year ending July 1, 2013, an influx of foreigners combined with a continuing decline in the loss of migrants to other states increased the population by more than 61,000, nudging it past 8.4 million for the first time, according to estimates to be released on Thursday by the United States Census Bureau.