A secure way of accessing the session ID in JavaScript is by setting a (preferably non-global) variable with the value of the session ID and use that variable in your scripts.
Using the session ID as form token enables an attacker to steal the session ID through XSS.