Search code examples
javascripttemplate-strings

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


Solution

  • The dollar sign comes before the opening curly brace:

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

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