“use strict” is a literal expression in JavaScript that was added in ECMAScript 5 (ES5). It helps out in a couple of ways:
1. Catching Common Coding Mistakes: “use strict” helps catch common coding mistakes and “unsafe” actions such as assigning a value to a read-only or non-existing global variable.
1. Avoiding Variable Leakage: In JavaScript, if you forget to declare a variable with “var”, “let”, or “const”, it automatically becomes a property of the global object (window in a browser, global in Node.js). This can lead to unexpected behavior and bugs. “use strict” prevents this by throwing an error when a variable is not declared.
1. Preventing the Use of JavaScript “Bad Parts”: Some features of JavaScript are considered “bad parts” or bad practices, such as with(), eval(), arguments.callee, and deleting variables, functions, or function arguments. “use strict” prevents the use of these features.
1. Making Code “Safer”: Since “use strict” is helping catch errors at an earlier stage of code execution, it contributes to making code “safer”, and thus, can be useful when working on large scale or team projects.
1. Improving Optimisation: Because strict mode code is more predictable, it can sometimes be optimized by engine better than equivalent code that’s not strict mode.
1. Making It Easier to Debug: JavaScript is dynamically typed and will often convert values implicitly, which can make it harder to debug. “use strict” changes that to let you catch likely errors sooner.
While “use strict” can make JavaScript safer and easier, it’s important to note that it might cause problems if added to existing code not designed for it. Strict mode can change semantics, and code that works without strict mode can break when you apply it. It’s best used in new code or thoroughly tested when added to existing code.