Yeah, the primary cause of plastic yellowing is UV light exposure. But the thing is, even short, intermittent periods of UV light exposure over the years, like if a school kid brought their Tamagotchi outside with them at recess, or someone left the Tamagotchi near a window where it just gets an hour of sunlight a day, etc. can kickstart the chemical reaction that causes the plastic to yellow. The plastic polymer can also start breaking down simply due to age, causing it to yellow a little, and that's really unavoidable. Although, the yellowing is usually to a much lesser extent than that caused by sun exposure.
The thing is, strong, direct sun exposure can wreak havoc with LCD screens sometimes, so if you are asking because you are thinking of buying a sun-damaged vintage Tamagotchi, I would ask the person selling you the Tamagotchi to show you that the screen functions properly!
A more modern Tama won't have nearly as much of a problem as the old ones - plastics are no longer made with the chemicals they used back in the '80s and '90s, and don't turn yellow nearly as quickly.