JavaScript Q&A Logo
JavaScript Q&A Part of the Q&A Network

What is strict mode in JavaScript and why is it useful?

Asked on Jul 20, 2024

Answer

Strict mode in JavaScript is a way to opt into a restricted variant of JavaScript, which helps you write cleaner code by catching common coding errors and preventing the use of certain problematic features.
<!-- BEGIN COPY / PASTE -->
        "use strict";

        function exampleFunction() {
            // This will throw an error because x is not declared
            x = 3.14;
        }

        exampleFunction();
        <!-- END COPY / PASTE -->
Additional Comment:
  • Strict mode is enabled by placing "use strict"; at the beginning of a script or a function.
  • It helps in catching common mistakes such as assigning values to undeclared variables.
  • It prevents the use of certain JavaScript features that are considered problematic or deprecated.
  • Using strict mode can make your code more secure and robust by enforcing better coding practices.
✅ Answered with JavaScript best practices.
← Back to All Questions