Best is to contribute to both but let’s say you have a cash crunch and you have only 8K that you can set aside to contribute to your retirement accounts. This is the money you are not going to need in the next 1-6 months.
First is the free money that you get with your 401k …that is the employer match. Never leave that. This is free money That is usually around 2-6% of your salary and is either 25-100% of the contribution you make.
So let’s say you are earning 70K and your employer matches up to 4% of your salary and 50% of the contribution you make. So you will have to make 401 contributions of 5.6K (4% x 70K / 50%) to gain all the employer matching contributions. This is free money and you do not want to leave it on the table.
Note that HSA is no tax in and no tax out. While 401K is no tax in and tax out!
So HSA is better.
So you had this 8K money that you could contribute and you contributed 5.6K in 401K so you employed can do the matching contributions to your 401k.
Now contribute the remaining money 8K-5.6K = 2.4K into your HSA.
This is, of course, assuming you have an HSA eligible plan.