951

I include the statement:

"use strict";

at the beginning of most of my Javascript files.

JSLint has never before warned about this. But now it is, saying:

Use the function form of "use strict".

Does anyone know what the "function form" would be?

Vikrant
  • 4,922
  • 16
  • 46
  • 68
Zhami
  • 18,307
  • 13
  • 46
  • 46

9 Answers9

1028

Include 'use strict'; as the first statement in a wrapping function, so it only affects that function. This prevents problems when concatenating scripts that aren't strict.

See Douglas Crockford's latest blog post Strict Mode Is Coming To Town.

Example from that post:

(function () {
   'use strict';
   // this function is strict...
}());

(function () {
   // but this function is sloppy...
}());

Update: In case you don't want to wrap in immediate function (e.g. it is a node module), then you can disable the warning.

For JSLint (per Zhami):

/*jslint node: true */

For JSHint:

/*jshint strict:false */

or (per Laith Shadeed)

/* jshint -W097 */

To disable any arbitrary warning from JSHint, check the map in JSHint source code (details in docs).

Update 2: JSHint supports node:boolean option. See .jshintrc at github.

/* jshint node: true */
Community
  • 1
  • 1
bdukes
  • 137,241
  • 21
  • 139
  • 173
  • 1
    In JSLint for Visual Studio it's the option: "Allow global ES5 strict" – Jowen Oct 25 '13 at 15:07
  • 10
    This doesn't make sense for Node applications, though. `-1` – bevacqua Nov 11 '13 at 17:02
  • 1
    Hi Nico, I updated the answer, for node you can type: /* jshint -W097 */ to disable this warning – Laith Shadeed Jan 05 '14 at 08:56
  • @LaithShadeed An alternative would be `/*jshint strict:false */`, to make it clearer what you're doing (unless there's a particular benefit to your numeric code that I'm not aware of) – bdukes Jan 06 '14 at 14:55
  • You are right and also globalstrict: true, http://www.jshint.com/docs/options/#globalstrict, but i did not check jshint source code to confirm if all of them will end up with same result. – Laith Shadeed Jan 23 '14 at 22:09
  • @KulbirSaini have you tested your change? From my quick testing, if you've configured JSHint to warn when you're missing `"use strict"` (because it doesn't warn by default), then setting the `node` option to `true` doesn't turn that warning off – bdukes Aug 03 '15 at 14:24
  • @bdukes here is what I have. Jshint v2.8.0 (via `grunt-contrib-jshint`). It warns about `'use strict'` by default. Setting `"globalstrcit": true` in .jshintrc or via `Gruntfile.js` suppresses that warning. Alternate way to suppress the warning is setting `"node": true`. But yes, I do get warning by default. Also `globalstrict` is set to `false` in example `.jshintrc` (https://github.com/jshint/jshint/blob/4c2d091b7e50ce2681ee48a104578bb969c189ae/examples/.jshintrc#L53) which I guess reflects the default behavior. – Kulbir Saini Aug 03 '15 at 14:44
  • So every single function I ever write in Javascript is supposed to have this line of boilerplate? – Noumenon Sep 25 '15 at 01:22
  • 2
    @Noumenon it's not really boilerplate, it's a directive that changes the environment your code is running in. That said, the new ES6 syntax (modules and classes) are strict by default (see http://www.ecma-international.org/ecma-262/6.0/#sec-strict-mode-code), so going forward this won't need to be littered everywhere. In the meantime, you can wrap all of your code in an [IIFE](http://benalman.com/news/2010/11/immediately-invoked-function-expression/) to only have to specify `"use strict";` once per file. – bdukes Sep 25 '15 at 14:12
  • I don't think writing IIFE around all files in a modern JS project is always a good practice. Of course, the isolation of your components is important, but writing it in the source code add extra-work, expose you to the possibility that one of the coders forget it sometimes, and don't help reading it. You won't need it anymore if you're correctly using your grunt / gulp to automatically add it at "build time". In my case, I switched to eslint, and I simply used ``"strict": [2, "global"]`` – noelm Jan 08 '16 at 17:00
  • Also, `/*jshint browserify: true */` will achieve the same effect, and should be used if browserify is used. Browserify, like nodejs wraps the file inside an IIFE. – sampathsris Aug 14 '17 at 10:58
219

If you're writing modules for NodeJS, they are already encapsulated. Tell JSLint that you've got node by including at the top of your file:

/*jslint node: true */
Zhami
  • 18,307
  • 13
  • 46
  • 46
  • 2
    FYI, this option works for JSLint, but JSHint doesn't turn off the strictness warning with this directive. For JSHint, try `/*jshint strict:false */` – bdukes Jan 06 '14 at 14:54
  • 4
    Worded for me with JSHint v2.9.2. Added `"node": true` to .jshintrc – RyanM Aug 14 '16 at 19:22
72

I'd suggest to use jshint instead.

It allows to suppress this warning via /*jshint globalstrict: true*/.

If you are writing a library, I would only suggest using global strict if your code is encapsulated into modules as is the case with nodejs.

Otherwise you'd force everyone who is using your library into strict mode.

Thorsten Lorenz
  • 11,293
  • 6
  • 48
  • 58
19

I started creating a Node.js/browserify application following the Cross Platform JavaScript blog post. And I ran into this issue, because my brand new Gruntfile didn't pass jshint.

Luckily I found an answer in the Leanpub book on Grunt:

If we try it now, we will scan our Gruntfile… and get some errors:

$ grunt jshint

Running "jshint:all" (jshint) task
Linting Gruntfile.js...ERROR
[L1:C1] W097: Use the function form of "use strict".
'use strict';
Linting Gruntfile.js...ERROR
[L3:C1] W117: 'module' is not defined.
module.exports = function (grunt) {

Warning: Task "jshint:all" failed. Use --force to continue.

Both errors are because the Gruntfile is a Node program, and by default JSHint does not recognise or allow the use of module and the string version of use strict. We can set a JSHint rule that will accept our Node programs. Let’s edit our jshint task configuration and add an options key:

jshint: {
  options: {
    node: true
  },
}

Adding node: true to the jshint options, to put jshint into "Node mode", removed both errors for me.

Community
  • 1
  • 1
qris
  • 7,006
  • 3
  • 36
  • 42
17

Add a file .jslintrc (or .jshintrc in the case of jshint) at the root of your project with the following content:

{
    "node": true
}
Sahil Ahuja
  • 1,395
  • 11
  • 12
16

There's nothing innately wrong with the string form.

Rather than avoid the "global" strict form for worry of concatenating non-strict javascript, it's probably better to just fix the damn non-strict javascript to be strict.

glycoslave
  • 358
  • 2
  • 7
1

I think everyone missed the "suddenly" part of this question. Most likely, your .jshintrc has a syntax error, so it's not including the 'browser' line. Run it through a json validator to see where the error is.

Dirigible
  • 1,112
  • 12
  • 10
  • 1
    No, it suddenly happened because the online JSLint service added that feature in 2010 when the question was asked. – Quentin Aug 09 '16 at 10:49
1
process.on('warning', function(e) {
    'use strict';
    console.warn(e.stack);
});
process.on('uncaughtException', function(e) {
    'use strict';
    console.warn(e.stack);
});

add this lines to at the starting point of your file

DB K
  • 41
  • 4
-4

This is how simple it is: If you want to be strict with all your code, add "use strict"; at the start of your JavaScript.

But if you only want to be strict with some of your code, use the function form. Anyhow, I would recomend you to use it at the beginning of your JavaScript because this will help you be a better coder.

Jason Stackhouse
  • 1,558
  • 2
  • 14
  • 19
  • 8
    I actually do get this error when I only put `"use strict";` at the top of my JS file, so this might not be entirely true. – moesef Jul 29 '13 at 23:31
  • @moesef That is because you have errors in your code. It's just there to help make your coding skills better better and make your code less "loose"... it will not accept undeclared variables etc... – Jason Stackhouse Aug 13 '13 at 20:19
  • 11
    @JasonStackhouse: Not true. JSLint won't accept the "global" form of `"use strict";`, where it's just placed at the top of your code. It only allows `"use strict;"` when wrapped in a function. (JS_Hint_ allows you to use the global form, though -- see answer above for the setting needed). – peterflynn Aug 28 '13 at 00:10