2

To enable strict mode in JavaScript it's needed to insert 'use strict' to a script. If I have several scripts, I need to add it to all. Maybe it can be added only once in settings? Not found any examples for now.

P.S. I know about add-ons that add statement to all scripts, it's not very convenient anyway.

2
  • the editor (VSC) settings have nothing to do with running your JavaScript. Use a shell script to add the line to all files, you can filter based on the content of line 1, grep and head -1 Commented Nov 16, 2022 at 12:49
  • 1
    Does using "alwaysStrict": true, in your jsconfig.json work for you? Commented Nov 16, 2022 at 19:28

1 Answer 1

5

To enable strict mode in JavaScript it's needed to insert 'use strict' to a script.

Or to use modules, which are strict by default. To tell the JavaScript parser (and VS Code) that a file is a module, add an import or export to it. You can have an empty export if the file is completely standalone:

export {}

...although usually, once you adopt modules, you just naturally end up with import and export to share functions and such between the modules, so you don't need something synthetic like that (or "use strict";).

Modules offer benefits beyond automatically being in strict mode, such as having their own top-level scope (rather than top-level code being in global scope) and, of course, declarative dependencies between files.

More about modules on MDN and in Chapter 13 of my recent book JavaScript: The New Toys.

Maybe it can be added only once in settings?

The problem with doing that is that VS Code's settings have no effect on the JavaScript engine that will ultimately be responsible for parsing and evaluating the code. That's why you need something in the file itself.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.