JavaScript is a powerful language. It can be used to do anything from making simple animations on a website to building complex user interfaces in modern web applications. But there are some pitfalls that JavaScript developers need to watch out for, one of which is the accidental breaking of code due to sloppy coding practices.
One way around this problem is adding “use strict” at the top of your code files, which enforces stricter rules about variable and function naming conventions as well as syntax errors. In this blog post we will discuss what “use strict” does in JavaScript and why you might want it enabled on your own projects!
ECMAScript 5.x has introduced the “use strict” directive. The JavaScript directive “use strict” is not a statement and is ignored by older versions of the programming language.
The purpose of “use strict” is to indicate that the code should be executed in under strict mode. For exmaple, developers cannot use undeclared variables.
All modern browsers except Internet Explorer 9 and lower support the use strict directive.
Some features of “use stric”:
- Disallows undefined global variables.
- Assignment failures in strict mode will throw errors.
- Attempting to delete the undeletable properties will result in an error
- When it comes to object literals, you should try your best to be unique. In other words, don’t use the same property name more than once in an object literal or clash will happen!
- Parameter names passed to a function must be unique. For exmaple, you cannot create a function with 2 i variables like function(i, i){}.
- Do not allow the use of octal notation for numbers.
- Do not allow the
with
keyword. eval
function will not introduce new variables.- Do not allow deleting of plain names.