What potential pitfalls or errors should be avoided when converting PHP variables to JavaScript?

One potential pitfall when converting PHP variables to JavaScript is not properly escaping special characters. This can lead to syntax errors or unexpected behavior in the JavaScript code. To avoid this issue, you should use functions like json_encode() in PHP to safely encode variables before passing them to JavaScript.

<?php
$php_var = "Hello, world!";
$js_var = json_encode($php_var);
echo "<script>var js_var = $js_var;</script>";
?>