if your above Javascript is working fine and you want to insert that into a page.
Kindly add the following code to your functions.php.
I have tested this code it’s working fine.
function handle_comment_error() {
?>
<script>
document.getElementById("comment-error").innerHTML = "Please enter your comment.";
</script>
<?php
}add_action('wp_head', 'handle_comment_error');
Let me know if you face any issues.