Minecraft wiki entry for Defeating_a_monument, associated text: A laser is their primary offensive attack. It takes several seconds to charge, emitting a distinctive sound and changing color from purple to yellow in the process, and does 4 points of damage when the charging completes.
There is no way to dodge the laser, but the guardian must maintain line-of-sight during the charging process. If you can get behind a wall or pillar, the attack is canceled and deals no damage. Circling narrow pillars therefore can be used to circumvent multiple attacks from all sides.
The laser has a maximum range of about 15 blocks. If you can get far enough away before the charging completes, the attack is cancelled and deals no damage.
If the player gets too close to the guardian, it is liable to abandon the attack and swim away.
Guardians also have a natural Thorns-like defense. If attacked while their orange spikes are extended, the attacker takes 2 damage.