-1

Example:
function greet (name, owner) { return Hello ${name==owner?'boss':'guest'} }

What is that and how does this work?

`${}`
code Zero
  • 115
  • 2
  • 8
  • 1
    Please take some time to [search through and review the documentation](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Template_literals) before asking questions here. – Marty Oct 18 '16 at 06:25
  • 1
    @Kaiido It's meant as more of a general "you will find these sorts of things outside of asking questions here" rather than a "you should have read the entire MDN reference first", if that's what you mean? – Marty Oct 18 '16 at 06:33
  • @Kaiido you are right I try to find that on google but it is not helpful so I posted that here. – code Zero Oct 18 '16 at 06:40
  • @Marty I had try that and then posted here. – code Zero Oct 18 '16 at 06:41
  • @HimanshuBisht, next time, for this kind of really improbable to help any future readers (which won't know what to search for either), you may want to try to ask on [chat](http://chat.stackoverflow.com/rooms/17/javascript) instead of posting a question. – Kaiido Oct 18 '16 at 06:42

3 Answers3

4

It's called a Template Literal. Inside ${} is an expression, which based on your post is a ternary operator. Basically if you call greet() with the same name and owner arguments, it will return 'Hello boss' and otherwise returns 'Hello guest'.

Another example:

var year = 2016;
var birthDate = 1994;
var str = `My age is ${year-birthDate}`; // My age is 22

Note that the string is inside back-ticks/back-quotes, not single quotes.

Rax Weber
  • 3,730
  • 19
  • 30
0

${} This is an expression language syntax if your javascript is inside a .jsp file.

Here is the link to know more.

https://www.tutorialspoint.com/jsp/jsp_expression_language.htm

Sumit Vairagar
  • 486
  • 3
  • 12
-1

the code uses ternary operator its just equal to:

if(name===owner){
return 'boss';
}
else{
return 'guest'
}