How to get PHP variable in JavaScript?

Here is the way to pass the value of a PHP variable into a JavaScript variable.

<?php
$php_var = "PHP";
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Test</title>
</head>
<body>
    <h1>Hello World !</h1>

    <script>
        const js_var = "<?php echo $php_var; ?>";
        console.log(js_var);
    </script>
</body>
</html>
Result of pass PHP variable into a JS variable

Access the PHP variable from an external JS file

<?php
$php_var = "PHP";
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Test</title>
</head>
<body>
    <h1>Hello World !</h1>
    <script>
        const js_var = "<?php echo $php_var; ?>";
    </script>
    <script src="./test.js"></script>
</body>
</html>
console.log(`From test.js => ${js_var}`)
Access the PHP variable from an external JS file