When desperate for cash, borrowing from a 401(k) retirement account can be tempting. There are real consequences for taking an early withdrawal or loan, though.

“If you’re thinking about borrowing from your 401(k) plan or any lending institution, be really strategic about your borrowing,” says Kim Jenson, UBS complex director in Chicago. “Understand why you think tapping into your 401(k) or borrowing money is important. Think about why you’re investing in your 401(k) in the first place. It’s because it’s a very efficient way to save for your retirement.”

Most people have a long-term goal of not working one day, which requires significant assets to fund what could be a 30-year retirement. 401(k)s are one of the best ways to save for retirement, and this savings builds fast if you contribute up to the $18,000 annual limit, take advantage of any company match and make annual $6,000 catch-up contributions if you’re over 50.

If you need cash, you’re able to dip into this savings early. Each plan has its own rules, though, and it’s important to understand these before tapping into this account.

Related: Going Solo Doesn't Mean Going Without a 401(k)

What’s a hardship withdrawal?

Hardship withdrawals can be used for medical expenses, to buy principal residences, tuition, to prevent eviction or foreclosure on a principal residence, burial or funeral expenses, and to repair damage to your home.

You can withdraw up to the amount of your elective contribution, not including earnings or matching contributions. This money is taxed like regular income and subject to a 10% penalty if you’re not yet 59.5 years old. Also, additional contributions are generally not allowed for at least six months after the distribution.

If you run the numbers, taking a hardship withdrawal of $10,000 will leave you with $6,300 after paying $2,800 in taxes (if you’re in the 28% tax bracket) and a $1,000 penalty. If you invested that money at 5 percent for 20 years, compounded monthly, you’d have about $27,100.

How do 401(k) loans work?

You can borrow 50 percent of your account balance, up to $50,000. Loans are typically repaid over five years, with longer terms allowed for primary residence purchases. While forgoing any investment earnings, you pay yourself interest at typically the prime rate plus a margin of 1 percent or 2 percent. When you leave your job, the balance becomes due in a lump sum, or it’s subject to tax and the 10 percent penalty if you’re not yet 59.5 years old.

This loan doesn’t have a credit check and the rates are competitive, but consider if your budget has room to continue making contributions while repaying the loan.

Related: 13 Reasons Why Your 401(k) Is Your Riskiest Investment

Should you take a loan or hardship distribution?

Loans are generally a better way to fund a financial need than a straight distribution, depending on why you need this money.

To clear debt, explore all alternatives before borrowing from your 401(k). If you owe on credit cards, for example, look to negotiate your interest rate with your credit card company.

“If you’re facing severe debt issues and on the brink of bankruptcy, the last thing you want to do is use this money to pay off debt,” says Kelley Long, certified public accountant in Chicago. “Your 401(k) money is protected in bankruptcy.”

A 401(k) loan only makes sense once you’ve worked with a credit counselor and exhausted all the alternatives and have a plan to solve budgeting issues.

Tapping your retirement account is a last resort for medical bills. First, try to negotiate with the hospital or medical billing company, which won’t affect your credit unless the bills go into collections. Consider a 401(k) loan only if you’re unable to negotiate and can’t afford the bills.

College tuition might seem a worthy reason to borrow from your 401(k) but there are other ways to pay and save for college. A 529 plan, for example, is a very attractive vehicle designed to help people save for education. You retain control of the funds in the plan and also receive a tax benefit.

Your children are also able to take advantage of grants, scholarships and loans as an alternative to tapping into your retirement. “When you take money out [of a 401(k)], that’s money that goes against getting a scholarship,” says Katherine Dean, managing director of wealth planning, Wells Fargo Private Bank.

Like people say, “You can borrow for school, but you can’t borrow for retirement.”

When buying a home, a 401(k) loan may make sense if the money increases your down payment to 20 percent so you can avoid paying mortgage insurance. Be sure that you’re not overextended with the payments for your new house combined with the 401(k) loan. You can also take a hardship distribution up to $10,000.

You can use your 401(k) to fund a business without paying taxes or penalties by setting up a Rollover for Business Startups, or ROBS. That allows you to roll over your existing 401(k) plan into a new plan that’s established in your business. Once this is complete, you can invest your retirement moneys in your new business. ROBS eliminate the need for small business loans and allow you to hold all the equity in your business, but these are complicated and are subject to taxes and penalties if the set up isn't right. .

Depending on the business, your retirement is at stake. Most people who use this structure already have sufficient funds for retirement. “They’re looking to take a risk to do something new,” says Dean.

Related: 4 Trends in 401(k) Plans Entrepreneurs Need to Know About