2017-01-30 45 views
-1

的下面幾行代碼被保存到hello.jsNode.js的初學者打嗝

var hello = "Welcome to node land"; 

console.log('${hello}') 

理想運行節點hello.js應印有

 Welcome to node land 

,但它只是打印

 $hello 
+0

您運行的是哪個版本的節點? –

+0

使用的版本是6.9.4 – abson

+1

如你所說,它不是打印'$(hello)'而不是'$ hello'嗎? –

回答

2

模板字符串文字使用反引號`,而不是單引號。

var hello = "Welcome to node land"; 
console.log(`${hello}`); 
+0

非常感謝幫助我完成這個疏忽。 – abson

2

您需要使用`(反向)字符來使用模板文字。

var hello = "Welcome to node land"; 
console.log(`${hello}`); 
1

有在做這個沒有任何意義:

console.log(`${hello}`); 

...因爲其他的答案主張。 `${hello}`完成的唯一的事情是將hello轉換爲一個字符串,但它已經是一個字符串

只是這樣做:

console.log(hello); 

如果你想hello與其他文字相結合,這樣你可以使用一個模板字符串:

var name = "abson"; 
console.log(`Hello, ${name}!`); 

...這將打印Hello, abson!