Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't use template strings in node.js

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?

like image 961
Electric Coffee Avatar asked Sep 29 '15 13:09

Electric Coffee


People also ask

Why is template literal not working?

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.

Can I use template literals in node JS?

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.

What is `` in JS?

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.

Are template literals for strings?

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).


1 Answers

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.

like image 128
3 revs Avatar answered Oct 09 '22 21:10

3 revs