Set current User in Javascript

Your code is most likely running too early, before Discourse is initialized.
How to fix this (or whether it needs a fix at all) depends on what you’re trying to do. In which context will the JavaScript code needing the username actually run?