javascript string interpolation / template literals / template strings

bwagner's icon

Using the js object I tried to employ string interpolation, but got the error: "js: midi2notename.js: Javascript SyntaxError: illegal character, line 11". I'm aware of the workaround to concatenate the generated string with "\n", but was wondering why there's this deviation from the standard
[Template literals (Template strings) - JavaScript | MDN]

Max Patch
Copy patch and select New From Clipboard in Max.

tyler mazaika's icon

Can't see your code (clipboard copy in Max doesn't get the external files). However, your issue is likely that Javascript in Max is ES5 (2009), so it does not have many modern niceties.

bwagner's icon

Thank you, Tyler! For the record, below is the js code. Is the specific JavaScript version in Max documented somewhere?

inlets = 1
outlets = 1
function msg_int(r) {
      const names =
        'C C# D D# E F F# G G# A A# B'.split(" ");
    // appending "\n", because apparently, there's no way to do this in Max! See:
    // https://cycling74.com/forums/carriage-returns-in-sprintf#reply-58ed1fd5c2991221d9cbf776
    const pitch_octave = names[r % names.length] + (Math.floor(r/12)-2)
    outlet(0, `${pitch_octave}\n`)
}
tyler mazaika's icon

It is hidden in sentence one of the Introduction article.

https://docs.cycling74.com/max8/vignettes/javascriptinmax ->
https://docs.cycling74.com/max8/vignettes/jsintro ->

"The js and jsui objects run Javascript version 1.8.5 inside of Max."

bwagner's icon

Thank you!