Fables Alive
Fables Alive

Reputation: 2808

Javascript function parameter in a Template Literal string issue

Im learning javascript using webtools of essential webbrowsers (toolset with F12). here is a show up example

function sayHello(n){
document.writeln($`Hello {n}`);
}
sayHello("Andy Anderson");
//undefined appears as a result.

I expected result as "Hello Andy Anderson" but I got an undefined

Upvotes: 0

Views: 92

Answers (2)

Sébastien
Sébastien

Reputation: 12139

The dollar sign comes before the opening curly brace:

document.writeln(`Hello ${n}`);

function sayHello(n){
    document.writeln(`Hello ${n}`);
}
sayHello("Andy Anderson");

Upvotes: 2

user8897421
user8897421

Reputation:

Dollar sign placement is odd. It should be in the string.

document.writeln(`Hello ${n}`);

The reason you didn't get an error was that the $ is likely defined as a function in your environment, so it was used as a "tag" for the template literal.

Upvotes: 3

Related Questions