Do we need to put “use strict” in external js files if our html file (which imports the external js files) already has “use strict” ?
And if our external js files do not have “use strict”, are they still “strict” within a HTML file that has “use strict” ?
Example:
<!DOCTYPE html>
<html>
<head>
<title></title>
<script>
"use strict";
function f() {
// calling File1 functions (File1 does not have "use strict"; at the top)
// are the File1 functions "strict"?
}
</script>
<script src="File1.js"></script>
<script>
//by the way.. is it strict here ?
</script>
</head>
<body>
</body>
</html>
You must put
"use strict";(or'use strict';) at the top of each script (or function) to make them strict. In your example, the functions inFile1.jswill not be strict, nor will the second block. See https://developer.mozilla.org/en/JavaScript/Strict_mode#Invoking_strict_mode for details.If this wasn’t the case, using strict mode could invalidate third-party scripts that you import, so it makes sense that strictness only applies to the scripts and individual functions that you explicitly specify.
For example:
external.js:test.html: