DEV Community

Kurapati Mahesh
Kurapati Mahesh

Posted on • Edited on

3 1

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
"use strict";
a = 3.14;
console.log(a);
Uncaught ReferenceError: a is not defined 3.14
"use strict"
let a = 3.14;
console.log(a);
2 Deleting a variable
'use strict';
let a = 10;
delete a;
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
'use strict';
function a(x, x) {
console.log(x,x);
}
a(10, 10);
Uncaught SyntaxError: Duplicate parameter name not allowed in this context 10 10
'use strict';
function a(x, y) {
console.log(x,y);
}
a(10, 10);
4 Octal numeric literals
'use strict';
let a = 010;
console.log(a);
view raw octalvalue.js hosted with ❤ by GitHub
Uncaught SyntaxError: Octal literals are not allowed in strict mode. 8
'use strict';
let a = 0o10;
console.log(a);
// OR
'use strict';
let a = parseInt('010', 8);
console.log(a);
5 Using variable name as eval or arguments
'use strict';
let eval = 10;
let arguments = 20;
console.log(eval + ' ' + arguments);
view raw evalArgs.js hosted with ❤ by GitHub
Uncaught SyntaxError: Unexpected eval or arguments in strict mode 10 20
'use strict';
let a = 10;
let b = 20;
console.log(a + ' ' + b);
view raw evalArgsFix.js hosted with ❤ by GitHub
6 `this` inside function - refers to the object that called the function.
"use strict";
function testFn() {
console.log(this);
}
testFn();
view raw thisInsideFn.js hosted with ❤ by GitHub
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`
'use strict';
eval('var a =10');
console.log(a);
view raw varinEval.js hosted with ❤ by GitHub
Uncaught ReferenceError: a is not defined 10 Avoid using eval.
8 Using `with` statement
'use strict';
function test(a) {
with (a) {
console.log(a)
}
}
test(20);
view raw with.js hosted with ❤ by GitHub
Uncaught SyntaxError: Strict mode code may not include a with statement 20 Avoid using it
9 Deleting undeletable property
'use strict';
delete Object.prototype;
Uncaught TypeError: Cannot delete property 'prototype' of function Object() { [native code] } at :2:1 false Avoid it
10 Using Octal Escape characters
'use strict';
let a = '\010';
console.log(a);
Uncaught SyntaxError: Octal escape sequences are not allowed in strict mode. Returns nothing Avoid it
11 Writing to read only property
"use strict";
const obj = {};
Object.defineProperty(obj, "a", {value:10, writable:false});
obj.a = 20;
console.log(obj.a);
view raw readonly.js hosted with ❤ by GitHub
Uncaught TypeError: Cannot assign to read only property 'a' of object '#' 10
"use strict";
const obj = {};
Object.defineProperty(obj, "a", {value:10, writable:true});
obj.a = 20;
console.log(obj.a);
view raw makeWritable.js hosted with ❤ by GitHub
12 Writing to get only property
'use strict';
const obj = {
word: "first",
get test() {
return this.word.toUpperCase();
}
};
obj.test = 'test';
console.log(obj.test);
view raw getOnly.js hosted with ❤ by GitHub
Uncaught TypeError: Cannot set property test of # which has only a getter FIRST
'use strict';
const obj = {
word: "first",
get test() {
return this.word.toUpperCase();
},
set test(x) {
this.word = x;
}
};
obj.test = 'test';
console.log(obj.test);
view raw getOnlyFix.js hosted with ❤ by GitHub
13 Using variable as reserved keywords
'use strict';
let public = 10;
console.log(public);
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.

Thanks.


💎 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
    @urstrulyvishwak

For further updates:

Follow @urstrulyvishwak

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay