PHP session variable is retrieved in Javascript but becomes undefined

I have two session variables that I retrieve in a Javascript code. This is how the code is set up:

        <p><?php echo $_SESSION['userid'] ?></p> --> This works and value is shown
        <p><?php echo $_SESSION['accesstoken'] ?></p> --> Value is also shown
        <script type="text/javascript">
            var userid = <?php echo $_SESSION['userid'] ?>;
            var token = <?php echo $_SESSION['accesstoken'] ?>;
            alert(userid); --> this works and shows pop up with value
            alert(token); --> this doesnt work and is undefined

This is the value of userid: 551234131

This is the value of my token: AAADAq39fEZA0BAAVJyvfZAiu1kIcaHG4SFVzuBWl3hXfC9W0g26JaqXwZAHuNdIhh2eFDkwyopunCsZCCW3jZADT8DQBjZCAdRTC5PkgtN4wZDZD

Before the token value is stored in the session variable it is actually held inside another javascript variable without any problem (i.e I can call that variable with alert() and the token is shown).

So transfering this value FROM javascript TO session variable = no problem. But transfering the same value FROM session variable TO Javascript = doesnt work.

At first I thought there was a problem with datatypes so I tried casting it to a string value but it doesnt work. Any idea on what could cause this situation?

Token is a string. So you need to put quotes around it:

var token = '<?php echo $_SESSION['accesstoken']; ?>';