Template Literals
In javascript, you can write a string using 'single quotes'
, or "double quotes"
, or backticks:
const msg = `this is also a string`
The nice thing about using backticks, is that you can refer to a variable directly using ${ ... }
syntax. Consider the following code:
const hello = 'world'
const ver_1 = 'regular quotes: hello ${ hello }!'
const ver_2 = `backticks: hello ${ hello }!`
console.log (ver_1)
console.log (ver_2)
... and the following sketch:
function setup () {
createCanvas (440, 150)
textAlign(CENTER, CENTER)
textSize (32)
noStroke ()
}
function draw () {
background (`turquoise`)
// using a template literal to construct a string:
const msg = `we are up to frame ${ frameCount }!`
text (msg, width / 2, height / 2)
}