What are the potential pitfalls of passing variables between PHP and JavaScript?

One potential pitfall of passing variables between PHP and JavaScript is the risk of exposing sensitive information to users. To prevent this, you can use PHP to sanitize and validate any data being passed to JavaScript to ensure it is safe to use.

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

<script>
var jsVariable = <?php echo json_encode($variable); ?>;
console.log(jsVariable);
</script>