
Originally Posted by
Elin
なんだ on its own can indeed mean "what" / "why" in casual language. But that's a different "なんだ” to the "なんだ" on the end of sentences, so it's best not to group them together in your head like that. When it comes at the end of a sentence like that, it's generally either a mild way of giving a reason / explanation for something, or stressing something. I think it's one of these things that is quite hard to explain the usage of beyond a simple definition like that, so it's best to just observe how Japanese people use it and try to mimic accordingly
An aside, but Japanese people quite often also use it as a get out clause if they start a sentence in plain form out of habit and then realise that the person they're speaking to is someone they have to be polite to, as you can end most plain form sentences with んです / のです / なんです and have them sound polite. :'D