Example:
function greet (name, owner) {
return Hello ${name==owner?'boss':'guest'}
}
What is that and how does this work?
`${}`
Example:
function greet (name, owner) {
return Hello ${name==owner?'boss':'guest'}
}
What is that and how does this work?
`${}`
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.
${} 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
the code uses ternary operator its just equal to:
if(name===owner){
return 'boss';
}
else{
return 'guest'
}