Why and when do Japanese speakers switch between formal and informal language among friends?


So I’ve read and noticed that Japanese speakers regularly switch from using だ
to です and using both plain and 〜ます forms very frequently throughout a
conversation even with friends. Why is there such a common change in formality
with people you’re close with or know well? And how do they decide which to
use when?

Edit: I don’t mean transitioning permanently, I mean in everyday conversations
where the switch is random. I have a friend who switches all the time while we
talk and I’ve seen street interviews where the formality is completely all
over the place. Why does it switch so often?

