How to declare a constant in Processing editor & p5.js? (reported issue)

Hi everyone. When declaring a constant, Processing Editor show me the following error message:

SyntaxError: expected an operator but found const

In Javascript, the first line (generating the error message in Processing editor) is the following:

const PASSWORD_MIN_LENGTH = 4;

When this message is displayed, it’s impossibe to execute and test the program into browser.

Do I something wrong? Thanks a lot for your insight!

Laurent

1 Like

Can you please post a MCVE?

Does this happen in other editors, such as a basic text editor or the alpha web editor?

Can you add "use strict"; as the first line in your sketch? Not sure if it would help, but that is that first thing I would try. I am also very curious to know what editor you are using.

Kf

Thanls Kevin. Actually, the following example works fine in browser and is editable on other editors like Alpha web editor, Notepad++ and Netbeans. All of them do not claims for a syntax error.

const PASSWORD_MIN_LENGTH = 4;

function setup() {
  createCanvas(400, 400);
	
	alert(PASSWORD_MIN_LENGTH);
	
}

function draw() {
  background(220);
}

But what I’ve just observed is that if I create a new p5.js sketch into Processing IDE and paste the above code, everything works fine in browser and editor. So I must investigate what’s the difference between my project code and my test code. It’s late but I’m gonna do it as soon as possible. In the same time, I’m gonna try to make a MCVE, good idea!

1 Like

Thanks Frajet! Unfortunalely no ‘strict’ effect. Editor used is simply the integrated editor of Processing IDE (Processing 3.3.7).

Just to note, the code you provided is an MCVE. It allows to demosntrate your approach, and it could be use to reproduce the problem, if any.

Kf

Kevin, is the following MCVE enough? I’ve done this MCVE by making a copy of the p5.js sketch from Processing IDE, with the ‘Save as…’ command. Still the same error. BUT when I create a new p5.js sketch into Processing IDE and replace the default .js and .html code with a copy of the MCVE code, the error does not occurs… I’m asking myself if this could result of a file corruption (problem of character encoding for instance)…

.js

const PASSWORD_MIN_LENGTH = 4;

var username;
var password;
var firstname;
var lastname;
var birthday;
var birthMonth;
var birthYear;
var gender;
var statusMsg = "";


function setup() {
  
   createCanvas(windowWidth, 50);

}

function draw() {
  
}

.html

<html>
<head>
  <meta charset="UTF-8">

  <!-- PLEASE NO CHANGES BELOW THIS LINE (UNTIL I SAY SO) -->
  <script language="javascript" type="text/javascript" src="libraries/p5.js"></script>
  <script language="javascript" type="text/javascript" src="libraries/p5.dom.js"></script>
  <script language="javascript" type="text/javascript" src="MCVE.js"></script>
  <!-- OK, YOU CAN MAKE CHANGES BELOW THIS LINE AGAIN -->

  <!-- This line removes any default padding and style. 
       You might only need one of these values set. -->
       
       
  <style>

    body {padding: 0; margin: 0; font-family:Arial, Helvetica, sans-serif;}
  
 </style>

</head>

<body>




</body>
</html>
1 Like

Yep, that’s a great MCVE. It’s interesting that this works for you if you create a new project and paste this code in.

Yeah could be. I’m not sure what could cause this, but if I were you I would probably just copy my code over into a new project and move on.

Thanks Kevin. I tried it yesterday (copy / paste solution), but it… failed. After this I’ve closed the Processing IDE, I’ve opened the *.js file with an editor (Atom, apparently the one used by Daniel Shifman into his famous videos). Everything worked fine at execution into browser and Atom editor didn’t point a syntax error. But if I reopen the corrected sketch back into Processing IDE I get exactly the same error. So it’s still a mystery. Bug into Processing IDE code parser? Should I put this thread into github Processing developpers forum?

You might consider filing an issue in the Processing editor GitHub here.

Thanks for your help :smile:! At least I’ve got an alternative for the moment. I’m gonna open an issue on GitHub. I wanted first to be sure it was not an error of mine. 'hope it could be corrected even if it’s not a priority issue.

This is a bug and it has been reported to the developpers. Apparently same problem when using let instead of var.

1 Like