[SOLVED] How to distinguish plain quoted string and template literal(backtick)

Issue

They are the same in the result, but they have different abilities. Is there any way to identify between them so that I can restrict the user(of a function) to use only template literal as an argument?

Solution

You mean something like an imaginary isFromTemplateLitteral boolean?

function(string){
  if(string.isFromTemplateLitteral()){
    // console.log("YES!")
  }
}

No can do.
Because the string was passed to the function using reference in the memory stack for it. So even before the function body starts running, string is already a normal string. A primitive with type string.

There is no way to know how it has been constructed (template litteral, concatenation, simple/double quotes, etc.) or where it really comes from (user input, harcoded text, regular expression match, etc).

Answered By – Louys Patrice Bessette

Answer Checked By – Marie Seifert (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *