In this tutorial, you will learn about the JavaScript ‘use strict’ syntax with the assistance of examples.

What is “use strict” in JavaScript?

“use strict” is an expression stating that JavaScript code ought to be executed in “strict mode.” The expression was presented in ECMAScript 5.

Strict mode” is a more confined adaptation of JavaScript, where semantics are adjusted to make the code stronger and secure. In “strict mode,” some quiet mistakes are changed to throw errors and disable some of the more befuddling or undefined highlights in JavaScript.

Why Strict Mode?

Strict mode makes it simpler to write “secure” JavaScript.

Strict mode changes previously accepted “bad syntax” into real errors.

For instance, in typical JavaScript, mistyping a variable name makes another global variable. In strict mode, this will throw a mistake, making it difficult to unintentionally create a global variable.

In typical JavaScript, a developer won’t get any mistake feedback assigning values to non-writable properties.

In strict mode, any task to a non-writable property, a getter-just property, a non-existing property, a non-existing variable, or a non-existing object, will throw a mistake.


‘use strict’; states that the JavaScript code ought to be executed in ‘strict mode‘. This makes it simpler to compose good and secure JavaScript code. For instance,

myVariable = 9;

Here, myVariable is made without declaring. This works as a global variable in JavaScript. Be that as it may, in the event that you use this in strict mode, the program will throw a blunder. For instance,

'use strict';

// Error
myVariable = 9;

The above code throws a blunder on the grounds that myVariable isn’t declared. In strict mode, you can’t use the variable without declaring them.

To demonstrate this program is in the strict mode, we have used

'use strict';

at the top of the program.

You can declare the strict mode by adding ‘use strict’; or “use strict”; toward the start of a program.

At the point when you declare strict mode toward the start of a program, it will have global scope and all the code in the program will execute in strict mode.


Strict Mode in Variable

In strict mode, using a variable without declaring it throws a mistake.

Note: You need to declare strict mode toward the start of the program. In the event that you declare strict mode underneath some code, it won’t work.

For instance,

console.log("some code");

// 'use strict' is ignored
// must be at the top
"use strict";

x = 21; // does not throw an error

Strict Mode in Function

You can likewise use strict mode inside a function. For instance,

myVariable = 9;
console.log(myVariable); // 9

function hello() {

    // applicable only for this function
    'use strict';

    string = 'hello'; // throws an error
}

hello();

On the off chance that you use ‘use strict’; inside a function, the code inside the function will be in strict mode.

In the above program, ‘use strict’; is used inside the hello() work. Subsequently, the strict mode is pertinent just inside the function.

As should be obvious, at the start of the program, myVariable is used without declaring.

On the off chance that you declare ‘use strict’; at the top of the program, you can’t use a variable without declaring it inside the function too. For instance,

// applicable to whole program
'use strict';

function hello() {
    string = 'hello'; // throws an error
}

hello();

Note : Strict mode doesn’t apply to block statements with {} braces.


Things Not Allowed in Strict Mode

  1. Undeclared variable is not allowed.
'use strict';

a = 'hello'; // throws an error

2. Undeclared objects are not allowed.

'use strict';

person = {name: 'Muskan', age: 25}; // throws an error

3. Deleting an object is not allowed.

'use strict';

let person = {name: 'Muskan'};

delete person; // throws an error

4. Duplicating a parameter name is not allowed.

"use strict";

function hello(p1, p1) { console.log('hello')}; // throws an error

hello();

5. Assigning to a non-writable property is not allowed.

'use strict';

let obj1 = {};

Object.defineProperty(obj1, 'x', { value: 42, writable: false });

// assignment to a non-writable property
obj1.x = 9; // throws an error

6. Assigning to a getter-only property is not allowed.

'use strict';

let obj2 = { get x() { return 17; } };

// assignment to a getter-only property
obj2.x = 5; // throws an error

7. Allocating to another property on a non-extensible object isn’t allowed.

'use strict';

let obj = {};
Object.preventExtensions(obj);

// Assignment to a new property on a non-extensible object
obj.newValue = 'new value'; // throws an error

8. Octal syntax is not allowed.

'use strict';

let a = 010; // throws an error

9. The variable name arguments and eval are not allowed.

'use strict';

let arguments = 'hello'; // throws an error

let eval = 44;

10. You can’t likewise use these saved watchwords in strict mode.

implements interface let package private protected public static yield


Advantages of Strict Mode

The use of exacting mode:

assists with composing a cleaner code

changes previously acknowledged quiet mistakes (bad syntax) into real blunders and throw a mistake message

makes it simpler to write “secure” JavaScript


Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.