Jshint.com requires "use strict". What does this mean?

JavascriptJshint

Javascript Problem Overview


Jshint.com is giving the error:

> Line 36: var signin_found; Missing "use strict" statement.

Javascript Solutions


Solution 1 - Javascript

Add "use strict" at the top of your js file (at line 1 of your .js file):

"use strict";
...
function initialize_page()
{
    var signin_found;
    /*Used to determine which page is loaded / reloaded*/
    signin_found=document.getElementById('signin_button');
    if(signin_found) 
{

More about "use strict" in another question here on stackoverflow:

https://stackoverflow.com/questions/1335851/what-does-use-strict-do-in-javascript-and-what-is-the-reasoning-behind-it

UPDATE.

There is something wrong with jshint.com, it requires you to put "use strict" inside each function, but it should be allowed to set it globally for each file.

jshint.com thinks this is wrong.

"use strict";    
function asd()
{
}

But there is nothing wrong with it...

It wants you to put "use strict" to each function:

function asd()
{
    "use strict";
}
function blabla()
{
    "use strict";
}

Then it says:

> Good job! JSHint hasn't found any problems with your code.

Solution 2 - Javascript

JSHint maintainer here.

JSHint—the version used on the website—requires you to use function-level strict mode in your code. It is very easy to turn that off, you just need to uncheck "Warn when code is not in strict mode" checkbox:

jshint.com screenshot

Why don't we allow global strict mode as suggested by @Czarek? Because some of the JavaScript files used on your page might not me strict mode compatible and global strict mode will break that code. To use global strict mode, there is an option called globalstrict.

Hope that helps!

Solution 3 - Javascript

I think its because jshint is trying to "protect" us against accidental assignment strict mode to entire file. And also it is good to wrap code with anonymous function, or use somekind of namespace.

e.g. both function in strict mode:

(function() {
    
   "use strict";
    
   function foo() {
        .....
   }
   
   function bar() {
        .....
   }
}());

Solution 4 - Javascript

JSlint requires your code to be in 'strict mode'

To do this simply add "use strict"; to the top of your code.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser656925View Question on Stackoverflow
Solution 1 - JavascriptCzarek TomczakView Answer on Stackoverflow
Solution 2 - JavascriptAnton KovalyovView Answer on Stackoverflow
Solution 3 - JavascriptKornel DylskiView Answer on Stackoverflow
Solution 4 - JavascriptMikeDView Answer on Stackoverflow