How to change the material in the game, depending on an event?

Well, I wonder how to change the material of an object in real time, eg when a car hit something, it changes the standard material for a material dent. Or when a character is hit, it changes the skin (material) to an injured skin (other equipment).

I wonder how in Javascript, please!

There are javascript examples on this page: