最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

syntax - Meaning of ${} in javascript - Stack Overflow

programmeradmin2浏览0评论

I need to work on javascript lately. Unfortunately, I am a newbie.
I have e across the following code and don't understand the meaning of ${count == 0}.

function body_onload()
{
    if(${count == 0})
    {
        document.getElementById("dispaly").style.display="none";
    }   
    scanImageReportFrom.shopCodes.focus();
}

Thank you.


Finally I found this that can solve my question.

I need to work on javascript lately. Unfortunately, I am a newbie.
I have e across the following code and don't understand the meaning of ${count == 0}.

function body_onload()
{
    if(${count == 0})
    {
        document.getElementById("dispaly").style.display="none";
    }   
    scanImageReportFrom.shopCodes.focus();
}

Thank you.


Finally I found this that can solve my question.

Share Improve this question edited May 23, 2017 at 11:56 CommunityBot 11 silver badge asked Jul 11, 2013 at 6:57 JohnnyJohnny 6333 gold badges9 silver badges21 bronze badges 5
  • 1 where have you found such a code? – Vadym Kovalenko Commented Jul 11, 2013 at 7:02
  • From the project I need to follow up. Those code was written by someone in my pany that I don't know. – Johnny Commented Jul 11, 2013 at 10:26
  • Ok. does project contains any javascript libraries? – Vadym Kovalenko Commented Jul 11, 2013 at 10:29
  • Like this? <script language="javascript" type="text/javascript" src="<%=path%>/js/mon.js"></script> – Johnny Commented Jul 12, 2013 at 1:21
  • That <%=path%> confirms the idea that you are looking at the raw source code rather than the generated output. – Álvaro González Commented Aug 8, 2013 at 7:09
Add a ment  | 

4 Answers 4

Reset to default 8

It's not you. :-) That's not valid JavaScript (the { triggers a syntax error).

It could perhaps be a token for some pre-processor that replaces it with something before the JavaScript is passed to the JavaScript engine.

In Javascript the ${} is used to insert a variable to a string.

var foo = "cheese";
console.log(`We want to eat ${foo}!`); // This needs the grave accent (`)
// Outputs "We want to eat cheese!"
console.log("We want to eat " + foo + "!"); 
// Outputs "We want to eat cheese!"

Sometimes the ${} method could be faster than using quotes.

Just to update this- it is also valid ES2015/ES6

MDN docs - template literals

let a = 4;
let b = 2;
console.log(`a is ${a} and b is ${b}`);

I was reading Template Literals, where in the 2nd para of description I found this:

Along with having normal strings, template literals can also contain other parts called placeholders, which are embedded expressions delimited by a dollar sign and curly braces: ${expression}

And further in Interpolation Strings I found:

Without template literals, when you want to bine output from expressions with strings, you'd concatenate them using the "+" (plus sign) (addition operator):

let a = 5;
let b = 10;
console.log('Fifteen is ' + (a + b) + ' and\nnot ' + (2 * a + b) + '.');
// "Fifteen is 15 and
// not 20."

That can be hard to read – especially when you have multiple expressions.

With template literals, you can avoid the concatenation operator — and improve the readability of your code — by using placeholders of the form "${expression}" to perform substitutions for embedded expressions:

let a = 5;
let b = 10;
console.log(`Fifteen is ${a + b} and
not ${2 * a + b}.`);
// "Fifteen is 15 and

// not 20."

More details of using "${}" in Template Literals is given in the documentation of the same

发布评论

评论列表(0)

  1. 暂无评论