What potential security risks are associated with passing PHP variables to JavaScript in this context?

Passing PHP variables directly to JavaScript can expose sensitive data or open up the possibility of XSS attacks if the variables are not properly sanitized. To mitigate these risks, it is recommended to use JSON encoding to safely pass PHP variables to JavaScript.

<?php
$php_variable = "Hello, World!";
$js_safe_variable = json_encode($php_variable);
?>

<script>
var js_variable = <?php echo $js_safe_variable; ?>;
console.log(js_variable);
</script>