5

I need to work on javascript lately. Unfortunately, I am a newbie.
I have come 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.

Community
  • 1
  • 1
Johnny
  • 633
  • 3
  • 9
  • 21

4 Answers4

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.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • @WesleyMurch: I did say "perhaps." :-) – T.J. Crowder Jul 11 '13 at 07:03
  • Then I really don't understand as it runs fine without any errors... :O – Johnny Jul 11 '13 at 07:10
  • @JohnnyTse: No, it doesn't. If that text is actually passed to a normal JavaScript engine, the engine **will** throw a syntax error. Completely gratuitous example: http://jsbin.com/azosam/1 (look in the JavaScript console, where it will say `Uncaught SyntaxError: Unexpected token {` or similar). Some browsers are *really subtle* about reporting JavaScript errors. All due respect, you're probably just not noticing it. :-) Try Chrome, press F12 to show dev tools, and click the Console tab. Or, as I said, something is pre-processing that before it's given to the engine. – T.J. Crowder Jul 11 '13 at 07:12
  • 1
    If it's being preprocessed, `view source` can be helpful in finding out what the post-processed code looks like. – beatgammit Jul 11 '13 at 07:20
  • I am not sure yet. I was trying to ask the writer about this through someone. But have not got the answer yet. Will tell the situation once I get the answer... – Johnny Jul 11 '13 at 09:13
  • @JohnnyTse: Okay. But again, with the code presented, there is no question, the syntax is invalid. – T.J. Crowder Jul 11 '13 at 09:20
  • Could not get answer from the writer... But seems it is really invalid syntax as I tried to delete it and the result is same. – Johnny Jul 15 '13 at 02:32
  • Delete the ${} is not same. When I load the homepage, there is error indicating that count is not indentified. I just did not notice the error. – Johnny Aug 08 '13 at 07:18
1

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}`);
Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
sferret
  • 361
  • 2
  • 14
1

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.

John Conde
  • 217,595
  • 99
  • 455
  • 496
Bugs Bunny
  • 47
  • 5
0

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 combine 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

aryyan
  • 1