Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

People also ask

Should I always use strict mode?

First, all of your code absolutely should be run in strict mode. Core modern javascript functionality is changed (see . call() and apply()) or disfigured (silent Errors) by executing code outside of strict mode.

What is the benefit of using use strict `?

First, strict mode eliminates some JavaScript silent errors by changing them to throw errors. Second, strict mode fixes mistakes that make it difficult for JavaScript engines to perform optimizations: strict mode code can sometimes be made to run faster than identical code that's not strict mode.

What is the function form of use strict?

“use strict”; is a string literal expression place on the first line of the javascript file or the first line in a javascript function. This line will be read and enforced by ECMAScript version 5(javascript 1.8. 5) or newer, and ignored by older versions of javascript.

What is the use of JSHint?

JSHint is a static analysis tool written in JavaScript that helps developers detect potential bugs in their JavaScript code and enforce their development team's JavaScript coding conventions.


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:

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.


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!


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() {
        .....
   }
}());

JSlint requires your code to be in 'strict mode'

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