0

I am testing my application for security purpose and while doing that I came across this scenario:

<script>
function myFunction() {
    alert`"Hello\nHow are you?"`;
}
</script>

Results in pop-up with message Hello How are you?

<script>
function myFunction() {
    alert("Hello\nHow are you?");
}
</script>

Results in pop-up with message Hello How are you?

Why these two scripts producing same result?

Use this link for testing: http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_alert2

Yeldar Kurmangaliyev
  • 33,467
  • 12
  • 59
  • 101
quintin
  • 812
  • 1
  • 10
  • 35

1 Answers1

4

This is called a tagged template string.

Template strings are a different way to write a String literal which has been introduced in ES6 which gives extra features, such as the one you describe.

The reason you're getting back your String in the alert is because alert is .toStringing the Object which was passed into it, which actually looks more like

["\"Hello\nHow are you?\""]
Paul S.
  • 64,864
  • 9
  • 122
  • 138