Dreamer Dreamer - 1 year ago 80
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?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download