Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

334
Views
ReferenceError for defined variables

I'm baffled. I imagine I've tread on some bit of JavaScript esoterica but I don't know what it is.

The code is

processMessage(ws, json){
    let msg = JSON.parse(json);

    console.log(`Message(${msg.type}): ${msg.data}`);
    console.log("Available games: " + msg.data); // Normally isn't here, put it in to make sure I wasn't going crazy

    switch (msg.type){
        case "welcome":
            console.log("Welcome received. Requesting available games.");
            let msg = {
                "type":"game list",
                "data":null
            };

            ws.send(JSON.stringify(msg));
            break;
        case "restore":
            break;
        case "game list":
            console.log("Available games: " + msg.data);
            break;
        case "create console":
            break;
    }
}

It generates this output

Message(welcome): undefined                                                                 MainUI.js:94:21
Available games: undefined                                                                  MainUI.js:95:21
Welcome received. Requesting available games.                                               MainUI.js:98:29
Message(game list): The Office                                                              MainUI.js:94:21
Available games: The Office                                                                 MainUI.js:95:21
Uncaught ReferenceError: can't access lexical declaration 'msg' before initialization       MainUI.js:109:21
    processMessage http://localhost:8000/MainUI.js:109
    onmessage http://localhost:8000/MainUI.js:44
    loaded http://localhost:8000/MainUI.js:42
    onload http://localhost:8000/:1

I'm pretty sure that output has msg, or its properties, being accessed 1,2,3...4 times before the error occurs.

Aaaaaand, I just found the cause. Knew it was my fault but there's the esoterica.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Within the switch itself, I put

case "welcome":
    console.log("Welcome received. Requesting available games.");
    let msg = {
        "type":"game list",
        "data":null
    };

    ws.send(JSON.stringify(msg));
    break;

That second let msg, instead of providing an error like 'msg' has already been declared, somehow confuses the interpreter into thinking that msg hadn't yet been declared. Removing let and just having msg = makes the error go away.

If anyone reads this and has a reference to explain the reason this happens, I'd really appreciate your adding a comment with a link.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!