Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

"use strict"

Do pro programmers code with strict mode on ? Can you explain strict mode?

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there, ah11 ! I received your request for assistance. Yes, I do tend to use "strict mode". This was introduced into new versions of JavaScript. The idea here is to keep your code as "safe" as possible. In strict mode, you cannot use a variable that is not previously declared, you cannot use names that have already been determined will be keywords in future iterations of JavaScript, you cannot delete a function (delete myFunction), nor a variable, you cannot delete a prototype from an object and a multitude of other things. This is entirely for security reasons and to help keep you from inadvertently doing something with your code that you never meant to do.

Imagine for a moment that you used a variable that would then later become a keyword in JavaScript 3 weeks down the line. All of your code would probably break and it might be a bit before you notice it. There are lots of examples on what you can and cannot do in strict mode. I highly suggest you take a look at the documentation here.

Hope this helps! :sparkles:

Michael Hulet
Michael Hulet
47,869 Points

These are all very good points, and to add onto it, a lot of our stack where I work is in JavaScript, and I don't think there's a single file that isn't marked with "use strict"

Zhaopeng Wang
seal-mask
PLUS
.a{fill-rule:evenodd;}techdegree seal-36
Zhaopeng Wang
Full Stack JavaScript Techdegree Graduate 32,210 Points
  • Yes, people do use strict mode in a production environment, because JS gives too much freedom and something would cause problems.
  • one of the main reason is that it forces you to declare the variable before actually using it. And it will remind you to put var in front, therefore, it helps not to mess up with the scope.
  • ESLint is also recommended in production environment