"But even if you have positive cashflow, it generally makes sense for young companies to reinvest those earnings instead of taking debt investment."
Not necessarily - or rather, it's often rational to do both.
It's rational for a business to take out a loan if the interest rate of the loan is less than their expected return on capital. Interest rates for business loans are what, 6-8%? It's not uncommon for a well-run, growing business with a strong franchise to earn a ROC of about 25%.
Debt lets them grow faster, sooner, and then they can pay off the debt from future earnings. It's often far more capital-efficient than floating an equity offering, where the investors would get a share of all those additional profits.
It doesn't work for web startups because web startups often have far lumpier earnings. It's not uncommon for a web startup to be bought for multi-millions before they earn a dime of profit. That's because their real "customers" are big companies, who are paranoid about losing their market to an upstart. A web startups users are effectively an advertising expense: spend a few thousand on bandwidth to show that if you wanted, you could take over the world, and then get a big company to buy you to prevent that from happening.
There are some web startups that employ debt financing very effectively. For example, Akamai floated close to $300M of convertible bonds to fund its expansion. As a result, it effectively acquired a lock on the content-delivery business, which has allowed it to grow by leaps and bounds over the past 5 years. Without that debt financing, they'd either have to forgo their thousands of datacenters in strategically-placed locations, or they'd have diluted their equity so much that it wouldn't be worth it for them.