DEV Community

Kurapati Mahesh
Kurapati Mahesh

Posted on • Updated on


Javascript: Why 'use strict';

Every concept in Javascript has too much information to keep in mind. Even "use strict;" as well. I can write lengthy article on it but I am a kind of guy usually ignore lengthy theoretical articles. So, this time I want to write article in different way but like one stop for "use strict;". No further words. Let's start:

What is "use strict;"?

To indicate code should run in strict mode - Thatz it. Yes 😊

How to use?

Everywhere in article writing it as "use strict;". Just place the same to the beginning of function or script.

When it is introduced in Javascript?

Do we really required?🧐 anyhow, In ES 5.

Please don't ask me, who introduced it. 😛

why do I use?

It makes to write secure Javascript. Short answers are not accepted for questions starting with Why😳.

Alright, Let's go deep but no theory.

S.No Action Example Strict Mode Non - Strict Mode Fix
1 Using variable without declaring it
Uncaught ReferenceError: a is not defined 3.14
2 Deleting a variable
Uncaught SyntaxError: Delete of an unqualified identifier in strict mode. false It can't be deleted. Variables created without var, let & const are deleted using delete.
3 Duplicate function params
Uncaught SyntaxError: Duplicate parameter name not allowed in this context 10 10
4 Octal numeric literals
Uncaught SyntaxError: Octal literals are not allowed in strict mode. 8
5 Using variable name as eval or arguments
Uncaught SyntaxError: Unexpected eval or arguments in strict mode 10 20
6 `this` inside function - refers to the object that called the function.
undefined Returns window object: Window {0: Window, 1: Window, 2: global,...... Avoid using this inside functions to not to expose window object.
7 Creating variable with `eval`
Uncaught ReferenceError: a is not defined 10 Avoid using eval.
8 Using `with` statement
Uncaught SyntaxError: Strict mode code may not include a with statement 20 Avoid using it
9 Deleting undeletable property
Uncaught TypeError: Cannot delete property 'prototype' of function Object() { [native code] } at :2:1 false Avoid it
10 Using Octal Escape characters
Uncaught SyntaxError: Octal escape sequences are not allowed in strict mode. Returns nothing Avoid it
11 Writing to read only property
Uncaught TypeError: Cannot assign to read only property 'a' of object '#' 10
12 Writing to get only property
Uncaught TypeError: Cannot set property test of # which has only a getter FIRST
13 Using variable as reserved keywords
Uncaught SyntaxError: Unexpected strict mode reserved word 10 Avoid using reserved keywords as variables

Oops, it took whole day to write. Anyhow, please do comment if I miss any or in case of improvements.


💎 Love to see your response

  1. Like - You reached here means. I think, I deserve a like.
  2. Comment - We can learn together.
  3. Share - Makes others also find this resource useful.
  4. Subscribe / Follow - to stay up to date with my daily articles.
  5. Encourage me - You can buy me a Coffee

Let's discuss further.

  1. Just DM @urstrulyvishwak
  2. Or mention

For further updates:

Follow @urstrulyvishwak

Top comments (0)

Need a better mental model for async/await?

Check out this classic DEV post on the subject.

⭐️🎀 JavaScript Visualized: Promises & Async/Await

async await