0

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

1
  • 2
    I know your learning so just an FYI, but document.write is something you'd avoid in production-ready JS code. You might be interested in looking into console.log while you're learning, or alternatively the DOM APIs for manipulation like .appendChild. Commented Jan 12, 2018 at 22:24

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

oh! I thought if usage is similar to C#.
2

The dollar sign comes before the opening curly brace:

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

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

1 Comment

I'd suggest fixing or eliminating that comment at the end, since it clearly no longer applies. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.