Dreamer Dreamer - 4 months ago 40
Javascript Question

NodeJS v6.9.1 variable in string doesn't work

Try to learn basic functions from NodeJS, stuck in append String to file with variable:

const fs = require('fs');
const os = require('os');

var user = os.userInfo();

fs.appendFile('test.txt', 'Hello ${user.username}!');

From test.txt I got:

Hello ${user.username}!

What goes wrong here?


You need to use back-tick (`) instead of '

fs.appendFile('test.txt', `Hello ${user.username}!`);

Template literals

Template literals are enclosed by the back-tick (grave accent) character instead of double or single quotes.