Osblog's JavaScript Corner : The Modern Mode

Words
594
Reading
3 min
Listen
Play
9y

Please refer to the official github repository of https://javascript.info here.

In this tutorial we will explore the concepts of JS code structure available here. We will try to explore in more detail and in terms a beginner can understand.

Strict Mode

Strict Mode is a JavaScript feature that allows you to place an entire program or just a function in a strict context. This mode allows for better error-checking into the code.

When you’re using Strict mode you have several restrictions including not being able to use a variable without declaring it, you can’t define a property more than once in an object literal, use a parameter name more than once in a function or write to a read-only property.

Strict Mode Advantages

Strict mode helps a web developer in a few ways.

  • It helps prevent errors when you take unsafe actions (for example gaining access to the global object)
  • It disables several functions that are not very specific and are confusing
  • It catches common throwing exceptions and coding bloopers

How to Declare Strict Mode

Strict mode can be easily declared by using “use strict”, or 'use strict’ at the beginning of either a program or a function, depending on which you want to place in the strict context.

If you declare script mode in a global context, all the code the program has will be in strict mode,
like in the example below.

"use strict";
// this code is in strict mode

You can also place “use strict” at the very beginning of a function. This way, the strict mode will only affect that specific function and not the whole program, the rest of** the code will be executed in normal mode**. However, this feature is usually used by most programmers for the whole script.

The example below shows the use of the “use script” feature for a specific function using the following syntax:

function(){
"use strict";
// ... the code ...
}

The script mode is accepted by all modern browsers. The oldest Internet Explorer version that supports this feature is Internet Explorer 10. The great thing about strict mode is that it is introduced by a simple statement that contains the words “use strict” and this way no new syntax needed to be introduced. This means that if you will learn to introduce this feature in all your code from now on, it will have** no side effect in older browsers**.

When you declare “use script” you automatically switch the engine into the modern mode and this will change the way some built-in features behave.

Restrictions

Variables

One of the most important restrictions imposed by the strict mode is the use of undeclared variables.

For example:

"use strict";
function testFunction(){
testvar = 4;

The above syntax will return an error while in normal mode, a new object is created.

Delete

In script mode, you can only delete user-defined array elements and object properties. If you try to delete anything else, you will get an exception. Also, when delete returns false in normal mode, it throws an exception in strict mode.

Duplicating a property

In strict mode, you can’t define a property more than once in an object literal. Multiple definitions of a property is not allowed in strict mode.

var testObj = { prop1: 34, prop2: 23, prop1: 34 };

The above syntax will return an error as strict mode doesn’t allow you to use multiple definitions. This is only applicable for object properties.

Conclusion:

We have talked about the strict mode in JavaScript and how it alters the output of your code. For more info refer here. If you liked this blog, wait for the next installment of Osblog's JavaScript Corner.



Posted on Utopian.io - Rewarding Open Source Contributors

Osblog's JavaScript Corner : The Modern Mode | Ecency