JavaScript Foundations - "Use Strict"

Well here we are, Week 6 of the codú 6 week article challenge!🎉🎉

This article is inspired by Niall and I have also just learned this and thought maybe you'd like to too.

This weeks article topic is - "use strict"

This neat little piece of code is something I have came to learn via The Complete JavaScript Course 2025: From Zero to Expert! by Jonas Schmedtmann

Lets dive into the use cases of use strict and what exactly it means.

What is it? use strict or Strict mode makes several changes to normal JavaScript semantics:

  • Eliminates some JavaScript silent errors by changing them to throw errors.

  • Fixes mistakes that make it difficult for JavaScript engines to
    perform optimizations: strict mode code can sometimes be made to run faster than identical code that's not strict mode.

  • Prohibits some syntax likely to be defined in future versions of
    ECMAScript.

(Definition via MDN "Strict Mode") Check it out here!

Why should you use strict mode?

Strict mode is best for error handling and is as far as I can see pretty standard practice. JavaScript by default can allow you to declare variables without warning.

Lets look at two examples to compare: ⚠️"use strict"; will only work in js files opened or hosted in the browser.

Example 1:

x = "I am x"
console.log(x);

What are we going to see in the console? I'll give you some options.

  • "I am x"
  • x is not defined
  • x i am

Image description

Did you get it right?

Now lets retry that with "use strict" at the top. Hazard another guess from the options above.

Example 2:

'use strict'
x = "I am x"
console.log(x);

What will it return?

Uncaught ReferenceError: x is not defined

Well why is that? By default JavaScript is going to assume our x = "I am x" is a string and reassingable. So everything is ok.

However we do not want this, what if you intended x to be a const and down the script you accidentally reassign it, well you will find that there is no error to be found in the console. You arent sure why you code is messing up, hours of debugging later (if its quite a large script) or short, I've been there ☹️. Just to come accross this little x and you see const is missing.

Behold the usefulness of "use strict";.

Strict mode at the most basic level ensures there is a const or let or var in front of your variables. If not you will find in the console the error shown above. This is great, hours of debugging down to minutes maybe even seconds. Why would you not use it?

This can be applied globally, so by that I mean place it at the very top of your document like this

Global

"use strict";

/*Javascript code bla bla bla*/

Or just in functions if you just want a bit more rules and live life by the edge outside of them 😑.

That looks like this Local

function JavaScriptIsCool () {
"use strict";

  let x = 12;
  console.log(x);
}
JavaScriptIsCool ();
y = 10;
console.log(y);

You'll notice by punching that code into a js file that y wont return a reference exception, that is because "use strict" is only active in the function.

'use strict';
function JavaScriptIsCool () {

  let x = 12;
  console.log(x);
}
JavaScriptIsCool ();
y = 10;
console.log(y);

Strict mode has a few other uses too.

  • Prevent misspellings
  • Prevent accidental deleting
  • Prevent duplicating parameter names in a function
  • Prevent writing to read-only properties

All of which you can find examples for in this handy little guide by Sentry.

Check it out!

Check out my portfolio too, why not?

Thanks for reading.. and use strict!!!

Use StrictJavaScriptError Handling6 Week ChallengeFoundations
Avatar for Roger O'Donnell

Written by Roger O'Donnell

Junior Web Developer

Loading

Fetching comments

Hey! 👋

Got something to say?

or to leave a comment.