7

What is the difference between:

(function () {'use strict';})(); 

and "use strict".

I don't understand when or why I would use one over the other?

I think that one declares the complete external JS document strict and the other makes a function strict.

My confusion comes when I see people enclose their complete external JavaScript in:

(function () {'use strict';})(); 

Why not just start the document with use "use strict"?

Lastly, I have tested an external JS doc using the same code. One, I used "use strict" and it worked.

The other I enclosed the complete JS document with (function () {'use strict';})(); and it did not work.

Why?

JavaScript is my first programming language and I am a little overwhelmed by its depth. I really appreciate your patience and professional guidance thank you for any help or direction you can offer me.

Thank you so much.

Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
Starkemp315
  • 151
  • 1
  • 1
  • 4
  • One applies to a single function, the other applies to every function in the file. Maybe applying it to everything is undesired. –  May 30 '19 at 14:27

2 Answers2

3

If you

"use strict";

At the top of the file, the entire file (that is, all code in the file/script tag, including third-party libraries [thanks VLAZ for clarification]) is strict.

Placing it inside an IIFE:

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

Means that only code inside the IIFE will be strict.

Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
  • @VLAZ "use strict" at the top of a file only affects that file. It doesn't affect other scopes. It would affect every script that is concatenated into a single file, but wouldn't affect other script files. ES2015 also introduced a module-level mode that affects every script in that module. –  May 30 '19 at 15:05
  • @Amy thanks. I have no idea where I picked that from, then - I re-checked the sources I thought I saw it in and didn't see it. Must have hallucinated it, I guess. – VLAZ May 30 '19 at 15:10
0

Well

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

is completely useless; it has no effect on anything.

At the beginning of a scope, like a function or a <script> block,

"use strict";

signals the interpreter that you want code interpreted in "strict" mode. If it's in a function that does nothing, it has no meaningful effect on anything. Of course, if the actual function you're talking about has more code than just the "use strict"; statement, then it means that that function will be interpreted in "strict" mode.

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • 1
    I originally downvoted you because the comment about it being useless was not needed and didn't help answer the question. I think the insinuation is putting the strict mode call at the top of a IIFE module that has code in it, but the OP excluded code for brevity. You added more information and I removed the downvote. I still think the OP means that there would be more code in the IIFE besides the strict call. – zero298 May 30 '19 at 14:31
  • 1
    @zero298 well making assumptions about what the OP posted is a thing that I've learned not to do after 10 years on this site; it's impossible to know what's really going on. Maybe the OP meant there would be more code, but maybe not. What I posted as an answer involved the exact code from the question. – Pointy May 30 '19 at 14:33
  • 2
    IMO, assumptions are particularly important to avoid with beginners. –  May 30 '19 at 14:35
  • 1
    Thank you so much, I really appreciate your help! @pointy Love your Van Eyke Avitar !! – Starkemp315 May 31 '19 at 15:06