There are some things you should do to avoid getting a virus:
1. NEVER, NEVER click on an attachment to an EMail unless you know that the sender is a trusted person/organization.
2. Avoid visiting "malicious" internet sites.
3. Just like EMail, NEVER click on a "questionable" link. unless (again) you know it is reputable.
Doing that will keep your Mac about 99% "safe". Sometimes, though, some adware/malware can "sneak" onto your machine. For that, there are two excellent programs you can use to scan your Mac for such "nefarious" stuff (and other "bad" things):
1. The free program Malwarebytes, available from here:
It goes "deeper" than Malwarebytes and can identify any viruses. You can use the Free Trial fi=or 30 days, and it has full functionality.
I myself purchased CalmXAV, and I run it about once or twice a month, just in case. I highly recommend it.
Additionally, it is best to perform disk cleanup/maintenance/repairs, from a software perspective. You can actually do a good amount of disk cleanup on your own, and there are some excellent programs available (both free and commercial) to help you with those tasks.
Then, of course, backups. It is imperative that you do backups to an external device as frequently as possible. The Mac OS comes with Time Machine, which does a good job. But two other backup/cloning programs, SuperDuper! and Carbon Copy Cloner, do an admirable job also. In case you are interested, here are links for each of them: