Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
字符串中的变量在javascript中不起作用_Javascript_String_Variables - Fatal编程技术网

字符串中的变量在javascript中不起作用

字符串中的变量在javascript中不起作用,javascript,string,variables,Javascript,String,Variables,代码如下: const textIn=fs.readFileSync('./txt/input.txt',utf-8') const textOut='这是一个加法:${textIn},\n在${Date.now()}'上创建的 fs.writeFileSync('./txt/output.txt',textOut) input.txt文件的内容是“hello world”。应该发生的是output.txt文件的内容应为“这是添加:hello world 1614606057967” 但是上面写

代码如下:

const textIn=fs.readFileSync('./txt/input.txt',utf-8')

const textOut='这是一个加法:${textIn},\n在${Date.now()}'上创建的

fs.writeFileSync('./txt/output.txt',textOut)

input.txt文件的内容是“hello world”。应该发生的是output.txt文件的内容应为“这是添加:hello world 1614606057967”

但是上面写着

这是添加项:${textIn}

已在${Date.now()上创建

你知道为什么吗?

根据,你需要把你的字符串用反勾号括起来,而不是用单引号括起来

所以你想要:

const textOut = `this is the addition: ${textIn}, \nCreated on ${Date.now()}`;

使用backticks(`)not inversed commaIt works,谢谢你的帮助!@sunday-很高兴我能帮上忙!如果这个答案解决了你的问题,你可以通过单击upvote和downvote按钮附近的复选标记来“接受”它,让其他人知道这个解决方案有效。