Because of the nature of JavaScript bad syntax can sometimes be allowed in your code without it reporting an error. In this video, we're going to talk about strict mode, strict mode forces errs for certain bad syntax that without strict mode would not be reported as errors. Now, in this course, that you've been going through, I have tried to emphasize good syntax. So there are certain bad syntax that's possible within JavaScript, which I have not included in this course, or I've warned you about. So that will help you prevent including some of that bad syntax, which will not report an error. However, I think using JavaScript strict mode is helpful in preventing those inadvertent uses of bad sins.
Tax. So let's quickly take a look at how you would do that if you choose to use JavaScript strictmode. Let me jump to sublime. Here I have a JavaScript file, I'm just going to create a simple function. All this function is going to do is x equal five plus six. And then of course, we'll execute it.
Now this is bad syntax. I have not declared this variable with the keyword var. If I put var in front of it, the syntax would be fine. Without var, it becomes a problem. And the reason it becomes a problem is because even though this variable is contained within a function without the keyword var it will become a global variable on the global space. And we want to limit our global variables to avoid problems or collisions with other code.
Now, even though this is bad syntax, if I save this, jump out to the page and refresh it, then open the console. There are no errors reported. Now let's take a look at how we would make this strict mode. There are two ways to incorporate strict mode into your JavaScript file. One is to place at the very top of your code, the phrase use strict inside of quotes, this is considered a directive. And all it will do is tell the compiler that you want to use strict mode.
Now the other way to use strict mode is to include it inside of the function at the very top of the function So right on this line. Now, the main difference between these two is if you use at the top of your code file, it applies to all of the code within that JavaScript file. Where if you use it at the top of a function is simply applies to the code within that function. Some have recommended that you should not place it at the top of your JavaScript files. Because if you concatenate or put your code with other code files, roll them up together, it could force all the code to use strict mode. That doesn't concern me as much as some as it does some other JavaScript developers, but I just mentioned it in case you want to follow that same practice.
Now let's see what happens. Well, let me just comment this out. So first, we'll use the one at the Top of the file, I save it, jump out, refresh. Sure enough, now we get an error. So that would prevent us from making that mistake. Same thing occurs if I just do it within the function, still getting the error.
Now, you may be asking yourself what exactly does strictmode turn into an error? Well, I'm going to refer you to the W three c schools, this URL that defines strict mode. It tells you how to use it and it gives some examples but it also identifies what is not allowed within strict mode. So you can refer to that page within w three schools for that information. That is strictmode and I recommend that you use it in your coding. Let's move on to the next topic and talk about JS lint