JavaScript is a very flexible language that often implicitly infer some certain things for programmers which makes it beginners friendly.

This certainly comes at a certain cost and with its own pitfalls which leaves room for buggy code in the production environment which can cause businesses to lose millions in revenue, this is where "use strict" comes in.

"use strict" is a simple JavaScript expression to make your code run in strict mode.

What this means when "use strict" is declared when you run your code is that you would be warned of possible errors, which would significantly reduce code bugs.

Here’s an example when a variable is declared without its keyword:

// when "use strict" is not declared

$ variableWithoutKeyWord = 1;
$ console.log(variableWithoutKeyWord); // returns 1

Here’s an example when a variable is declared without its keyword in strict mode:

// when "use strict" is declared
$ "use strict";

$ variableWithoutKeyWord = 1;
$ console.log(variableWithoutKeyWord); // ReferenceError: variableWithoutKeyWord is not defined

// This warns you that that you are calling a variable that you have not properly defined

Here’s an example when a variable is declared with its keyword in strict mode:

// when "use strict" is declared
$ "use strict";

$ var variableWithKeyWord = 1;
$ console.log(variableWithKeyWord); // returns 1 which works correctly

Here’s an example when a function is called without strict mode:

// when "use strict" is not declared

$ function testFunction() {
  val = 200;
  return val;
}

$ testFunction(); // this returns successful without an error

Here’s an example when a function is called with strict mode:

// when "use strict" is not declared
$ "use strict";

$ function testFunction() {
  val = 200;
  return val;
}

$ testFunction(); // this returns this error ReferenceError: val is not defined

// which means val is not properly defined

The examples above shows when strict mode is used global scope. Note that "use strict" can also be used in the local scope.

As shown in the example below:

Here’s an example when a function is called with strict mode:

// when "use strict" is not declared
$ valOne = 100;

$ function testFunction() {
  "use strict";
  valTwo = 200;
  return valTwo;
}

$ testFunction(); // this returns this error ReferenceError: valTwo is not defined

/**
* Which means valTwo is not properly defined in the testFunction scope. 
* Also note that valOne is also not properly defined but did not return an error. 
* This happened as a result of declaring "use strict" locally within our testFunction and not in the global scope of our code.
*/

Finally, there are other great checks "use strict" helps us identify when writing our code other than the ones explained above.

By using "use strict" expression to enable strict mode in our JavaScript code can save us from wasting a lot of time debugging thousands of lines code which makes it a very cool feature in JavaScript.