What is the purpose of embedding JavaScript in PHP code?

Embedding JavaScript in PHP code can be useful for dynamically generating JavaScript code based on PHP variables or conditions. This allows for more flexibility and customization in client-side scripting. By embedding JavaScript within PHP code, you can easily pass data from the server-side to the client-side and execute JavaScript functions based on server-side logic.

<?php
$variable = "Hello World";
?>

<script>
  var message = "<?php echo $variable; ?>";
  alert(message);
</script>