saveStrings Reference Error

The p5.js “saveStrings” Reference says:

let words = ‘apple bear cat dog’;
let list = split(words, ’ '); < < ! ! This is wrong

iI needs to be… let list = words.split(’ ');

This threw me for a while before I looked hard.

“The split() function maps to String.split(), …”
https://p5js.org/reference/#/p5/split

It’ll work fine within the setup(), draw(), etc. functions. However, it won’t work in regular JavaScript, i.e. in the global scope of your sketch.

Why have it have 2 different syntaxes then?

The String.split() will work anywhere. It’s a standard JS method, so you could just stick to using that. I suspect that the p5.js developers added a split() function to replicate the original Processing one. Recall that p5.js is a JavaScript interpretation of Java Processing.

Here’s more on the technical reasons why the split() function won’t work before setup()https://github.com/processing/p5.js/wiki/p5.js-overview#why-cant-i-assign-variables-using-p5-functions-and-variables-before-setup

1 Like