it is weird that. where "use strict" I place will have different result.
My node version is v9.9.0
I don't understand, would somebody help me
"use strict";
function tryFunction() {
var tryValue = 123;
return tryValue;
}
if (true) {
testvar = 123; // ReferenceError: testvar is not defined
}
function tryFunction() {
var tryValue = 123;
return tryValue;
}
"use strict";
if (true) {
testvar = 123;
}
// no errors???
function tryFunction() {
var tryValue = 123;
return tryValue;
}
if (true) {
"use strict";
testvar = 123;
}
// no errors???
See e.g. the MDN documentation for strict mode:
Strict mode applies to entire scripts or to individual functions.
[...]
To invoke strict mode for an entire script, put the exact statement
"use strict";(or'use strict';) before any other statements.
[...]
Likewise, to invoke strict mode for a function, put the exact statement
"use strict";(or'use strict';) in the function's body before any other statements.
(Emphasis mine.)
If "use strict"; appears in the middle of a file or block, it has no effect and is ignored as any other string literal would be.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With