2outube
Plus Search Batch YouTube
Transcript

#35 Strict Mode in JavaScript | Fundamentals of JavaScript | A Complete JavaScript Course

Summary

This video explains JavaScript's strict mode, a feature that helps write more secure code by converting previously accepted bad syntax into real errors. It demonstrates how strict mode catches common mistakes like undeclared variables, misspelled variables, duplicate function parameters, and the use of future reserved keywords, preventing silent failures and potential bugs.

Key points
  • Strict mode in JavaScript helps write more secure code by treating certain bad syntax as real errors.
  • Without strict mode, JavaScript might create global variables implicitly or allow misspelled variables, leading to silent bugs.
  • To enable strict mode, the string 'use strict' must be the very first statement in the JavaScript code.
  • Strict mode prevents the use of duplicate parameter names in functions.
  • Strict mode disallows the use of variable names that are reserved keywords for future JavaScript versions.
Chapters
Notable quotes (4)

“strict mode changes previously accepted bad syntax into real errors”

“the global object in JavaScript when we use JavaScript in browser”

“and to avoid these kinds of issues we can run this JavaScript code in strict mode”

“without strict mode JavaScript will simply fail silently without letting us know that we did a mistake”

0:01before finishing up this first section where we learned about the fundamentals of JavaScript let's learn about One More Concept which is strict mode in JavaScript a strict mode in JavaScript is a special mode which makes it easier to write secure code in simple words strict mode changes previously accepted bad syntax into real errors let's try to understand it with an example so let's say I declare a variable here let's call it you user and to this I assign a value John now if you notice I have not used let const or V keyword to declare this variable if I save the changes you will see we don't
0:40have any error now here we don't have any error because when we don't use let const of V keyword to declare a variable in that case that variable gets created on the global object when we use JavaScript in browser the global object is the window object and the window object is nothing but the browser Tex tab which we are using in this casee window object will point to this browser tab okay so here this user this variable will be created as a property on the window object and window object is the global object in JavaScript when we use JavaScript in browser so if I go ahead and if I log
1:21window. user so window is the global object on that if I try to access user if I save the changes you you will see John has been logged that means this user here it is created as a property on this window object and this window object is the global object in JavaScript when we use it in the browser so as you can see here when we have not used let con or V keyword to declare this variable here we are not getting any errors in the same way let's say I have created a variable using let keyword let's say is eligible let me set it to
2:00True okay then let's also create another variable let's call it is full age and let's set it to True okay and here let's say is eligible is false now what I will do is I'll write an if statement here and here we will check is full age if it is true in that case we are going to set is eligible to true okay and after this line let's simply go ahead and let's log is eligible okay now here let's say I misspelled this variable name instead of calling it is eligible I use uppercase i here so this variable it is different from this is eligible variable if I save
2:50the changes you won't see any error and you will see that when I'm logging this is eligible with the correct variable name it is logging false that's because here the value which we are setting for this is eligible that is eligible is different from this is eligible variable which we have created here so here I have misspelled the variable name we have not declared this is eligible variable anywhere but still I am able to set its value and JavaScript is not complaining about it this is another issue and this can lead to some unforeseen bugs and to avoid these kind of issues we can run this JavaScript
3:29code in strict mode and to run the JavaScript code in strict mode we can use this string use strict and that's it and now if I save the changes you will see first we have this error user is not defined because here we are trying to Define this user this user variable without any let con of V keyword so now when we have set this strict mode by using this string use strict now JavaScript is complaining that this user variable it is not defined so now we can fix it so let's say I create this variable using let keyword if I save the changes we should
4:09not have this error anymore but now we have another error and now it says this is eligible is not defined so you see when we have set strict mode now it is complaining about all the issues which we have in our code so now I can go ahead and I can change this variable name here which I misspelled to is eligible and if I save the changes that error should also be gone so this is the use of strict mode now always remember that this statement this us strict it should always be the first statement in your JavaScript code no other statement or no other
4:51JavaScript code should come before this us strict for example before this use stct if I use let's say some console.log statement okay and now if we have any error in our code this used strict is not going to work again it is not going to complain about anything so this use strict it should always be the first statement you see again I'm not using let const over keyword to declare this variable but now this UST strict is not complaining about anything that's because here this us strict is not the first statement in this JavaScript code it should always be the first statement okay so now if I move it above
5:31the console.log statement in that case again it will start working because again this UST strict is the first statement in this Javascript file all above statements are commands Okay so again if I save the changes it should complain and it should give us this error user is not defined so let's again go ahead and let's define this variable declare this variable using let keyword and now if I save the changes it should not give us that error so basically strict mode forbids us to do certain things it will create visible errors for us in certain situations in which without strict mode JavaScript will simply fail silently
6:14without letting us know that we did a mistake and we saw two examples of that the first example was when we created a variable without using let const of our keyword and the second example is when we misspelled the variable name while assigning it a value another example would be if we create a function let's say let me call this function as gr you can name this function anything inside this function I will simply write a console.log statement and here let's say this is greeting function okay a simple function and this function let's say takes two parameter here I'm going to specify two parameter may be x and x I'm not using this
6:57parameter in this function I've just created it now without strict mode if I comment this strict mode for now and if I save the changes you won't see any error but if I UNC commmand this strict mode and if I save the changes you will see that we have an error and it says we have duplicate parameter name and duplicate parameter name is not allowed so without strict mode when we specified the same parameter names it was not giving any error but in strict mode it is giving us that we have a duplicate parameter name and and it is throwing it as an error so
7:31now as a developer I can go ahead and I can change the name of the second variable to something else and now we should not have this error so you see how this strict mode helps us developers in finding and fixing errors in our program another example would be if I try to create a variable with a name which might be a keyword reserved for future JavaScript version that will also not be allowed for example in JavaScript currently we don't have interfaces but in future in the future version of JavaScript we might have interface introduced in JavaScript but currently it is not there so if I use interface as
8:15the variable name and if I assign it with some value let's say 100 without strict mode we should not get any error if I save the changes there is no error but if I uncomment this strict mode and now if I save the changes you see we have an error and it says unexpected strict mode reserved word so this interface it is reserved for future versions of JavaScript so in strict mode I'm not able to use this as a variable name and this use case is because we might want to run this JavaScript which we are writing today in the future browsers and let's say in the future
8:56JavaScript has introduced interface keyword for creating the inter interfaces at that time when we will run this JavaScript code in the future browser which supports interface that time it will consider this variable interface as the interface keyword and at that time this code will fail so that's why when we use strict mode JavaScript does not allow us to use any keyword reserved for future JavaScript version okay so this is the use of strict mode to enable strict mode all we have to do is we have to write this string use strict at the very beginning of the Javascript file it should always be the first statement in the Javascript
9:40file all right so I hope now you understand what is strict mode and why it is useful for you as a developer and how to use it this is all from this lecture if you have any questions then feel free to ask it thank you for listening and have a great day e
Works on any YouTube video. Swap youtube.com for 2outube.com in the address bar — or paste a link.
Read another video
#35 Strict Mode in JavaScript | Fundamentals of JavaScript | A Complete JavaScript Course

Email this transcript

The full transcript as Markdown, straight to your inbox. Nothing else — no list, no follow-ups.

Keyboard shortcuts

Search the transcript/
Copy plain textc
Slow down / speed up[ ]
Show this help?
Close / clear searchesc