According to MDN, Template Strings should be working in Chrome, and by extension V8 on which Node.js is based on; but when I try the following I get a syntax error:
var name = 'coffee';
console.log(`Hello, ${name}!`);
running node file.js
just results in a SyntaxError: Unexpected token ILLEGAL
Is there some kind of flag I need to enable to use this feature, or is it simply not implemented in node?
JavaScript template literals require backticks, not straight quotation marks. 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.
You can also use the String. raw() function to do that which is a built-in JavaScript tagged template literal that allows you to declare raw strings without processing the escape characters.
Although single quotes and double quotes are the most popular, we have a 3rd option called Backticks ( `` ). Backticks are an ES6 feature that allows you to create strings in JavaScript. Although backticks are mostly used for HTML or code embedding purposes, they also act similar to single and double quotes.
Template literals are sometimes informally called template strings, because they are used most commonly for string interpolation (to create strings by doing substitution of placeholders).
Template strings were added in NodeJS v4.0.0. And I can confirm that they do work at least as far back as v4.1.1; I didn't bother to check v4.0.0, but I have no reason to doubt the linked announcement. No special runtime flag is required.
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