It's a tempting but risky move. Many plans let you borrow up to $50,000 or up to half your accrued balance, typically the lesser of the two. The interest rates are also attractive, but there are perils to be mindful of.
First, if you wind up no longer working with your employer, then you usually have to pay back the full amount within two months. If you have anything outstanding after that, there are multiple tax penalties.
Depending on the plan, you might not be able to continue making contributions to the plan. A lack of contributions might negate the pretax benefit and leave you with a larger tax bill. You also might be leaving matching contributions on the table.
In some cases, money borrowed does not remain invested, so it is not compounding, generating interest, or growing with the market. This can seriously delay retirement.
Depsite all this, a 401(k) loan can make sense if you are consolidating several higher interest loans into one, in order to lower your total monthly obligations.
Other potential advantages of a 401(k) loan are the lack of a credit check to affect your rating, no additional costs to the loan, and no penalty for paying the loan back faster than scheduled.
It's a complicated decision, but hopefully knowing all these factors will help you make an informed choice. The devil is always in the details, so make sure you know your plan's rules inside and out.