"why" can be compared to an old Latin form qui, an ablative form, meaning how. Today "why" is used as a question word to ask the reason or purpose of something. This use might be explained from a formula such as "How does it come that ...".

Understanding the Context

If you meet an old friend of yours, whom you never expected to meet in town, you can express your surprise by saying: Why, it's Jim! This why in the ...