Capsaicin is an irritant chemical. Plants evolved it to avoid mammals eating them. It is advantageous for plants to be eaten by birds: they fly and defecate seeds far away. If a mammal eats a plant, it pounds ground somewhere not far away. Therefore, plants evolved a chemical that irritates mammals but does not irritate birds, namely capsaicin.
Then humans came and discovered they actually enjoy the irritation.
Latest Answers