Comments are used to provide explanatory notes within the code, enhancing code readability and understanding.