7

Is it possible, perhaps by using some directive in the JavaScript code, to throw warnings or errors if variables are used without being declared first?

In case that isnt possible, is there perhaps some plugin to force Eclipse (or any other IDE) to detect undeclared variables?

thunderboltz
  • 3,387
  • 3
  • 21
  • 20

2 Answers2

8

Yes, it's possible to do this using strict mode. You enable it by putting a statement containing the string literal "use strict" at the top of a file or function to enable strict mode for that scope.

"use strict";
doesNotExist = 42; // this throws a ReferenceError

This feature is now supported by all updated browsers. Older browsers wont' throw an error since "use strict"; is a valid statement and is simply ignored by browsers that don't support it. You can therefore use this to catch bugs while developing, but don't rely on it throwing an exception in your users' browsers.

Strict mode

JavaScript's strict mode is a way to opt in to a restricted variant of JavaScript, thereby implicitly opting-out of "sloppy mode". Strict mode isn't just a subset: it intentionally has different semantics from normal code.

Strict mode for an entire script is invoked by including the statement "use strict"; before any other statements.
(Source, Documentation)

ashleedawg
  • 20,365
  • 9
  • 72
  • 105
hammar
  • 138,522
  • 17
  • 304
  • 385
  • As far as I know, Strict Mode has only been implemented in Firefox. I can not find it anywhere else. Strict Mode is dangerous because of how its scope works. It applies to included files, but not inside functions. – yakatz May 26 '11 at 04:38
  • @yakatz: There is at least partial support in Chrome. [Try this test](http://java-script.limewebs.com/strictMode/test_hosted.html) to see how well it is supported in your browser. – hammar May 26 '11 at 04:50
  • Chrome 12 (beta): 37/38. IE9: 0/38 ;-) – yakatz May 26 '11 at 04:54
  • @yakatz, If "use strict" appears at the top of a program/file, then it applies to all functions declared in that program/file. If it appears at the top of a function body, then it applies to that function and all closures defined in that function. – Mike Samuel May 26 '11 at 05:15
  • 4
    If all Strict Mode did was force declaration of variables before use it wouldn't matter if it only worked in Firefox because (for this particular purpose) you could test against that browser - presumably finding out quite quickly where you've missed declarations. However, Strict Mode does other things as well so it's not a very good solution to this issue. A better solution is a validator like www.jslint.com or www.javascriptlint.com - although for some reason the latter considers checking for undeclared variables an "advanced" feature where I would want it by default. – nnnnnn May 26 '11 at 05:18
2

Edit: This answer is now incorrect; see "use strict"; per answer above (but JSLint is still handy).

This feature is akin to VB/VBA's Option Explicit and PHP7's declare(strict_types = 1);.


The feature you are looking for is sometimes called Option Explicit in other languages (I think it comes from Visual Basic). JavaScript does not have it. If you are looking for a way to check your variable usage, try JSLint.

ashleedawg
  • 20,365
  • 9
  • 72
  • 105
yakatz
  • 2,142
  • 1
  • 18
  • 47
  • 2
    +1 for JSLint and similar tools - Javascript has many more pitfalls then just accidental globals to worry about as well. – hugomg May 26 '11 at 05:20