I wanted to try using template literals and it’s not working: it’s displaying the literal variable names, instead of the values. I am using Chrome v50.0.2 (and jQuery).
console.log('categoryName: ${this.categoryName}\ncategoryElements: ${this.categoryElements} ');
${this.categoryName} categoryElements: ${this.categoryElements}
If you are using JavaScript's new template literal syntax in a JSP page it might not work as expected. That's because JSP and JavaScript both use the ${myVar} syntax.
Template Strings significantly simplify multiline strings. Simply include newlines where they are needed and BOOM. Here's an example: Any whitespace inside of the backtick syntax will also be considered part of the string.
Description. Template literals are enclosed by backtick (`) characters instead of double or single quotes. Along with having normal strings, template literals can also contain other parts called placeholders, which are embedded expressions delimited by a dollar sign and curly braces: ${expression} .
Template literals are a new feature introduced in ECMAScript 2015/ ES6. It provides an easy way to create multiline strings and perform string interpolation. Template literals are the string literals and allow embedded expressions.
You need to use backticks (otherwise known as "grave accents" - which you'll find next to the 1 key if you're using a QWERTY keyboard) - rather than single quotes - to create a template literal.
Backticks are common in many programming languages but may be new to JavaScript developers.
Example:categoryName="name"; categoryElements="element"; console.log(`categoryName: ${this.categoryName}\ncategoryElements: ${categoryElements} `)
Output: VM626:1 categoryName: name categoryElements: element
See: Usage of the backtick character (`) in JavaScript
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With