Here's one scenario: a person starts with $10,000 in the bank earning 0%, a $35,000 student loan balance, $60K/yr salary, no state tax and $25K/yr expenses not including federal tax. Any positive annual cash flow goes into that same 0% bank account. Net worth = $10K - $35K = -$25K.
Assume student Loan interest rate = 7% and the loan term = 10 years. That gives a monthly payment of $406.38. In the first 12 months, total payments will be $4,877: $2,371 interest, $2,506 principal.
The $10,000 is invested in a taxable fund that yields 7% in interest and short term capital gains, so $700 ordinary income.
With standard deduction and 1 exemption, that's a taxable income of $48,179 and a federal tax of $7,901.
At the end of the year, net worth =
-$32,494 (SL principal)
+$10,700 (taxable fund balance)
+$60,000-$4,877-$7,901-$25,000 (annual cash flows)
= $428.
If, instead, the $10K is used to pay down the loan principal to $25K, there is no $700 income and the same $406.38/mo will include $1,648 interest and $3,229 principal.
With standard deduction and 1 exemption, that's a taxable income of $48,202 and a federal tax of $7,907.
At the end of the year, net worth =
-$21,771 (SL principal)
+$0 (fund balance)
+$60,000-$4,877-$7,907-$25,000 (annual cash flows)
= $445.
...and all the above merely confirms the fact that, even accounting for taxes, pre-paying a student loan that has X% interest rate can be essentially identical to investing the same prepayment in a taxable account that earns the same X%. Because the student loan savings are guaranteed, however, from a risk-adjusted perspective it is better to pay the SL than try for a similar return in a non-guaranteed investment.
But...the OP noted "many posts advising people to pay student loans before fully funding tax advantaged investment accounts (401k, Roth, etc)." (italics added)
That's a different question. In the first example above, assume the $10000 was put into a 401k and earned the same 7%.
With standard deduction and 1 exemption, that's a taxable income of $37,479 and a federal tax of $5,226.
At the end of the year, net worth =
-$32,494 (SL principal)
+$10,700 (401K fund balance)
+$60,000-$4,877-$5,226-$25,000 (annual cash flows)
= $3,103.
So that looks much better! ...except...when that 401k balance is withdrawn, the IRS will take its cut. Assume the person is in the same 25% bracket when that happens, and subtract 25% * $10,700 from $3,103, and we get...$428, same as the first example. So the same logic applies: take the guaranteed return if the risky return is, on average, about the same.
Lots of assumptions and calculations above. I think the calculations are correct - if not, please advise. I know that using different assumptions is likely to lead to different conclusions, so no quarrels with different conclusions if based on different assumptions.