How to set "use strictly" worldwide using JSLint

I am new to javascript and trying to check through JSLint. Where should I use "strictly adhere" to use it all over the world and check?

This gives me the error "Unexpected expression" uses a strict position in the position of the operator. ":

"use strict"; console.log('doing js in head-section'); function helloWorld() { console.log('called function helloWorld()'); alert('Hello World from a JS function showing an alert!'); } function helloMyNumber() { console.log('called function helloMyNumber()'); var max = 42; var yourLuckyNumber = prompt('Enter your lucky number (between 1 and '+ max +')'); var myLuckyNumber = Math.floor(Math.random()*(max+1)); var paragraph = document.getElementById('luckynumber'); paragraph.innerHTML = paragraph.innerHTML + ' Your lucky number is: ' + yourLuckyNumber + '. Mine is: ' + myLuckyNumber + '. They ' + (yourLuckyNumber == myLuckyNumber ? 'DID ' : 'did NOT ') + 'match!'; } console.log('doing JS in body-section'); document.writeln('<p class="green">Hello World from JS within a body-section in HTML!</p>'); 
+7
javascript jslint use-strict
source share
2 answers

According to the documentation, the browser parameter for JSLint automatically disables the use of "use strict"; at the global level. As far as I know, there is no way to get it back.

You can disable the browser option and get the same predefined globals as the browser parameter using:

 /*global Audio, clearInterval, clearTimeout, document, event, history, Image, location, name, navigator, Option, screen, setInterval, setTimeout, XMLHttpRequest */ 

Alternatively, you can wrap all your code in IIFE and use "use strict"; up.

Alternatively, you can switch to JSHint (has more options) and use the strict: global parameter to enable "use strict"; on the global sphere.

+4
source share

'use strict' is usually used at the beginning of functions. For your code, I would just wrap it all in IIFE, which would make "use strict" valid

 (function() { "use strict"; console.log('doing js in head-section'); function helloWorld() { console.log('called function helloWorld()'); alert('Hello World from a JS function showing an alert!'); } function helloMyNumber() { console.log('called function helloMyNumber()'); var max = 42; var yourLuckyNumber = prompt('Enter your lucky number (between 1 and '+ max +')'); var myLuckyNumber = Math.floor(Math.random()*(max+1)); var paragraph = document.getElementById('luckynumber'); paragraph.innerHTML = paragraph.innerHTML + ' Your lucky number is: ' + yourLuckyNumber + '. Mine is: ' + myLuckyNumber + '. They ' + (yourLuckyNumber == myLuckyNumber ? 'DID ' : 'did NOT ') + 'match!'; } console.log('doing JS in body-section'); document.writeln('<p class="green">Hello World from JS within a body-section in HTML!</p>'); })(); 
0
source share

All Articles