"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 ...".

Context Explanation

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 ... In the sentence "Why is this here?", is "why" an adverb? What part of speech is "why?" I think it modifies the verb "is", so I think it is an adverb.

Insight Material

So, why does she ever tweet with standard capitalization (and do so quite randomly and quite frequently)? In contrast to the other answers, my answer (typing in only lower case is simply faster) explains the above tweets quite simply: