"Why, of course!" Why "why"?

Does anyone know the origin/meaning of the word “Why” in expressions such as “Why, yes!” and “Why, of course!”?

It’s not something you really hear nowadays, but it seems to have been common in the past, at least among fairly well-to-do English types, if the dialogue in old books is to be believed.

The question word “why” seems to be an odd choice for an all-purpose exclamation like that.


A: Do you have a public restroom?
B: Why [would you ask such a silly question?], of course [we do, it’s a public library].


B: But, of course [it’s reasonable that you might not know that.]

My reading of JD Salinger indicates that ‘but’ was used for emphasis in the middle of the last century.

“Did you see that dress? Man it was green! But green!”
“You ask me that everyday. But everyday!”

I’m not at home, so those aren’t actual Salinger quotes. I always assumed that’s where we got ‘But of course’. (And I keep waiting to read something like, “She was ugly. But ugly!”.)

So my WAG on ‘why’ is that it is also used for emphasis. As to the origin, beats me.