Wakefield's Daily Tips

Uncaught SyntaxError: Illegal return statement

This error appears in the Developer Tools console if you have an extra } bracket in Begin Experiment code.

The line that gives the error in this case is the return statement from experimentInit(), which is the function containing the extra bracket.

async function experimentInit() {
  // Initialize components for Routine "trial"
  trialClock = new util.Clock();
  } // extra bracket
  text = new visual.TextStim({
    win: psychoJS.window,
    name: 'text',
    text: 'Any text\n\nincluding line breaks',
    font: 'Arial',
    units: undefined, 
    pos: [0, 0], draggable: false, height: 0.05,  wrapWidth: undefined, ori: 0.0,
    languageStyle: 'LTR',
    color: new util.Color('white'),  opacity: undefined,
    depth: -1.0 
  });
  
  // Create some handy timers
  globalClock = new util.Clock();  // to track the time since experiment started
  routineTimer = new util.CountdownTimer();  // to track time remaining of each (non-slip) routine
  
  return Scheduler.Event.NEXT;
}

If the extra bracket is in End Experiment code, then the broken function is quitPsychoJS().

async function quitPsychoJS(message, isCompleted) {
  // Check for and save orphaned data
  if (psychoJS.experiment.isEntryEmpty()) {
    psychoJS.experiment.nextEntry();
  }
  } // extra bracket
  psychoJS.window.close();
  psychoJS.quit({message: message, isCompleted: isCompleted});
  
  return Scheduler.Event.QUIT;
}

If the extra bracket is in End Routine the the error changes to Uncaught SyntaxError: Unexpected token '}'. Check which function is giving the error to narrow down the location of the bracket.

1 Like