DEV Community

Discussion on: IIFE's (Immediately-Invoked Function Expressions) in JavaScript Explained in 3 Minutes

Collapse
 
mathieuhuot profile image
Mathieu Huot

Hi Fred, I think your article is very well written and it made me review my knowledge about JavaScript strict mode, so thank you for that! Now, it might just be a wording thing but I wanted to clarify that use strict will not prevent the assignment of variables in the global scope, it will only prevent the assignment of undeclared variables.

'use strict';
//The assignment or reassignment of a declared variable will not throw an error.
let globalVariable = 'global value';
globalVariable = 'another value';
//This code will not throw an error.
Enter fullscreen mode Exit fullscreen mode
'use strict';
globalVariable = 'global value';
//This will throw -> ReferenceError: assignment to undeclared variable globalVariable
//Note that if you remove "use strict" it will not throw an error but create a global variable instead.
Enter fullscreen mode Exit fullscreen mode

Here's a reference to what I'm describing in the code block above : https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Erreurs/Undeclared_var?utm_source=mozilla&utm_medium=firefox-console-errors&utm_campaign=default

You're right that because use strict is preventing the accidental creation of global variables, it reduces the risk of name collision although it doesn't prevent the assignment of a declared variable, global or not. Again, thank you for the learning opportunity!