如何在javascript函数中传递全局变量

Let say I have a $_SESSION['password'] and a javascript function.

Here's the javascript function:

<script>  
    function check() {
        var password = '<?php echo $_SESSION; ?>';
        alert(password);
        if (document.FormName.password.value != password)
            alert("password does not match");
    } 
</script>

<html>
//form here
</html>

How come when the alert pop-ups, it will only display nothing? what happened? Is my passing of variable wrong at all?

$_SESSION is an array and this will return blank if you echo it.

You should use the

<?php echo $_SESSION['password']; ?>

It will echo the password that store in Session.

<script>
    function check() {    
        var password = "<?=$_SESSION['password']?>";//you forgot ['password'] here       
        alert(password);
        if(document.FormName.password.value != password)
            alert("password does not match");
    } 
</script>

<html>
//form here
</html>