If I enclose use strict with backticks/template literals, 'use strict' is not working as expected. Can you share the reason behind it? Are there any similar exceptional statements where template literals are not going to work as expected?
`use strict`;
x = 3.14; // Ideally it should cause an error (as x is not defined).
alert(x);
`use strict`; // if we enclose in single quotes or double quotes
x = 3.14; // Ideally it should cause an error (as x is not defined).
alert(x);
That's because the Use Strict Directive is explicitly defined in the specification as an ExpressionStatement consisting entirely of a StringLiteral production, and limits the exact code point sequences to be either
"use strict"or'use strict'.From the ECMAScript 2020 Language Specification:
Emphasis added
On the other hand, a TemplateLiteral is an entirely different production than a StringLiteral, and therefore cannot be a valid directive.