ES5 has introduced a new mode, called strict mode, to help you catch these errors. When you are in strict mode, you can’t assign a value to a variable that hasn’t been declared. Here’s how you put your code into strict mode:

You can use “use strict” at the global level, as well as inside individual functions. If you put “use strict” at the global level, it affects all your code. If you put it inside a function only, it will affect just the code in that function.

So you could put all your strict code into an IIFE, Let’s look at one strict mode

<script>

(function() { 

"use strict"; 

var innerMyVar = "I'm not declared in this function either."; 

var o = { x: 3, x: 10 }; })();

</script>

We added an object, o, and we defined the property o.x twice in the object definition. Let’s see what happens in strict mode.

you see an error (in Chrome):

Uncaught SyntaxError: Duplicate data property in object literal not allowed in s
trict mode

Strict mode can help you write cleaner and better code. Strict mode will help you with other tasks as well; check out the MDN Developer Network’s strict mode page for more.

The way we tell the browser to use strict mode might seem a little odd; after all, it’s just a string, “use strict.”

They did it this way because so that older browsers that don’t support strict mode could safely ignore the statement.

To older browsers, “use strict” is simply a string and won’t affect how your code runs at all. To newer browsers, of course, the string causes the browser to go into strict mode.

This means you can use strict mode in your code without worrying that older browsers won’t be able to run the code. However, pay particular attention if you are linking to multiple scripts in your page.

You can combine scripts if they are all strict, or all non-strict, but you can’t mix the two! Make sure you know the mode of all of your scripts use before linking to them.

This article is written by our awesome writer
Comments to: what’s new ES5 in javascript

Your email address will not be published. Required fields are marked *

Attach images - Only PNG, JPG, JPEG and GIF are supported.

New Dark Mode Is Here

Sign In to access the new Dark Mode reading option.

Join our Newsletter

Get our monthly recap with the latest news, articles and resources.

By subscribing you agree to our Privacy Policy.

Latest Articles

Explore Tutorials By Categories

About

Codeverb is simply an all in one interactive learning portal, we regularly add new topics and keep improving the existing ones, if you have any suggestions, questions, bugs issue or any other queries you can simply reach us via the contact page

Login

Welcome to Codeverb

Ready to learn something new?
Join Codeverb!

Read Smart, Save Time
  •  
    Strength indicator
  •  
  •  
    Log In | Lost Password