Johnny
Johnny

Reputation: 633

Meaning of ${} in javascript

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.

Upvotes: 5

Views: 21788

Answers (4)

aryyan
aryyan

Reputation: 1

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

Upvotes: 0

sferret
sferret

Reputation: 461

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}`);

Upvotes: 1

Bugs Bunny
Bugs Bunny

Reputation: 47

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.

Upvotes: 1

T.J. Crowder
T.J. Crowder

Reputation: 1075567

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.

Upvotes: 8

Related Questions