How to check an element has particular css class or not

How to check an element has particular css class or not.
  1. if ($('#myElem').hasClass('myClass')) {  
  2.             alert('Has class');  
  3.     }