samsung s4 extremely hot
Unless you know for sure it became hotter than what it is designed for, I doubt you phone is damaged. One thing you didn't mention is where this temperature reading comes from ? If it reads the CPU temperature, 40°c is not that hot at all, a typical CPU can usually raise close to 90°c and I'm pretty sure the same is true for a phone CPU.

From my experience, the issue I have encountered on some occasions is that it looks like an application is stuck while using all the CPU resources without the user being aware. When it happens, I'm not using the phone at all and it is just using the battery and getting warm until I notice the heat. Then I usually need to reboot for this issue to be resolved. It doesn't look like my phone was permanently affected by these issues.