Indeed, that is the main function of sunglasses. The surface of the sunglasses is coated. The special material of the sunglasses can reflect the uv from the sunlight which can protect our eyes from this damaged rays. That is why most people tend to wear the sunglasses under the sunshine in summer.
Yes, the sunglasses do work for preventing UV rays. Although sunglasses have the functionality of blocking UV rays, there are 3 main categories of sunglasses that can protect different degree of UV rays. Firstly, cosmetic sunglasses have lightly lenses that can prevent no more than 60% of sunlight and UVA UVA rays. This type is not suitable for wearing when you are driving in the day. The second one is called general purpose sunglasses, which can block 60% to 90% of visible light and UVA rays. It's effective for you to protect eyes when you are driving. The last one is special purpose sunglasses that block up to 97% sunlight and UVA rays. It's useful when you exposure under the sun for a prolonged period, but not for driving.
The ability to block UV light is not dependent on the darkness of the lens or the price tag. While both plastic and glass lenses absorb some UV light, UV absorption is improved by adding certain chemicals to the lens material during manufacturing or by applying special lens coatings. Always choose sunglasses that are labeled as blocking 99-100% of UV rays. Some manufacturers' labels say "UV absorption up to 400nm." This is the same thing as 100% UV absorption.