Do you know what "use strict" does in JavaScript?
"use strict" mode - Explained

I am a Front end developer currently working on React JS, HTML, CSS, Python, Vue JS, Webpack, Babel, AEM etc.
Search for a command to run...
"use strict" mode - Explained

I am a Front end developer currently working on React JS, HTML, CSS, Python, Vue JS, Webpack, Babel, AEM etc.
I always wondered what use strict but never looked into it. Thanks for the explanation
Glad you liked it.
This series talks about Tips & Tricks and simple projects you can build using JavaScript. Click on the articles below to read more.
How to delete an element in an array using splice and delete and what is the difference between them both?.
Are you planning to start your coding journey?. Then this is for you - a roadmap to help you achieve your goals.

7 ways in which you can add files to your commit using git add

As a website user, one would have come across links or buttons which say "Click to download" or "Download" on a website. Have you ever wondered how can you add one to your website?. Keep reading!!!. As part of this blog let us see how one can crea...

Display Hello World and custom message using two-way data binding

Build a random quote generator using an API - JavaScript

Kritika Pattalam Bharathkumar | Web Developer - The official blog of Kritika Pattalam Bharathkumar
22 posts
Front End Engineer | Writes about HTML, CSS, JavaScript and everything related to web development | Follow me on Twitter let's chat.
As JavaScript developers, we often have "use strict" - strict mode enabled in our projects. But do we know what that expression does? Let's find out.
JavaScript's strict mode makes JS be executed in strict mode, which helps identify common mistakes in coding practices which might lead to potential bugs.
Syntax
"use strict;"
As part of this blog, let's see a list of things to remember while using strict mode in JavaScript
Declaring strict mode
Declaring a variable/object in strict mode
Deleting a variable/object in strict mode
Naming variables in strict mode
Duplicating a parameter in a function
Octal numeric literals are not allowed
Eval function to create a variable
Writing to a read-only property is not allowed
Writing to a get-only property is not allowed
'this' in strict mode
The "use strict" directive/expression is only recognized at the beginning of a script or a function.
Strict mode can be declared at the top of the file as the first thing. When declared at the top it takes a global scope. In the below piece of code, use strict is declared as the first thing on the script file, making it a global scope, causing the strict mode feature to be enabled for the whole file.
"use strict";
alert("I am part of the file");
function test() {
console.log("javascript use strict is declared at the top of the file");
}
test();
When used inside a function it has local scope and only that function will execute in strict mode. In the example below, the strict mode will be enabled only inside the function(local scope), and the code outside it will not follow the strict rules.
function log(x) {
"use strict;"
console.log(x);
}
log(5);
In the example below, use strict is not the first line & should be avoided. Preferably it's better to declare it on the top of the file due to global scope, instead of declaring it inside a function.
alert("I am inside this file");
"use strict;" //not declared as the first line
console.log("javascript use strict is not declared at the first thing");
Using a variable/object without declaring it is not allowed in strict mode. In the example below, myVar variable is assigned a value before declaring, this will throw an error.
What happens if strict mode is not enabled? - myVar will just be created as a global variable in that scenario.
"use strict;"
myVar = 5; // This will throw an error
function log(x) {
console.log(x);
}
log(5);
Deleting a variable or object or function when in strict mode enabled is not possible.
use strict;
let myVar = 5; // This will throw an error
function log(x) {
console.log(x);
}
log(6);
delete log; //throws an error
delete myVar; //throws an error
When learning JS for the first time, the thing which we are sure often would have read is "Do not use reserved words as variable names".
But at times people do tend to use reserved words without realizing it, in such cases strict mode helps by throwing an error. Just try it out in a chrome developer tool console and one should be able to see the difference.
"use strict;"
var static = "hello";
//static is a reserved word, hence when using strict mode, it will throw below error
// Uncaught SyntaxError: Unexpected strict mode reserved word
The example below shows that the x parameter in the function log has been duplicated. This is not allowed in strict mode.
use strict;
// This will throw an error because "x" parameter is duplicated
function log(x,y,x) {
console.log(x);
}
log(6,10);
In the example below, one might assume a preceding zero before 1 does nothing and the value might just be 12, but in JavaScript preceding zero before a number means its Octal numeric. Strict mode does not allow octal numeric.
'use strict';
// Will throw an error
let x = 012;
"use strict";
eval("var x = 1");// error
In the example below, we define an object, and in the configurable property, we set the writable value to false, which means that a particular object is read-only. So when you try to assign a new value to that object, the strict mode will throw an error.
'use strict';
let obj = {};
Object.defineProperty(obj, "x", {value:0, writable:false});
// Will throw an error
obj.x = 25;
In the example below, trying to write to a get-only property throws an error.
'use strict';
let obj = {get x() {return 0} };
// Will throw an error
obj.x = 25;
In non-strict mode, when you call a function that isn't bound to any object "this" will refer to the global window object, whereas in strict mode it returns undefined.
'use strict';
const foo = function() {
console.log(this); // undefined in strict mode
foo();
The concludes what is strict mode is, how/where to declare it, and the list of few items to keep in mind/expect while using "use strict" in JavaScript.
Follow me on Twitter | LinkedIn for more web development related tips and posts. Feedbacks and suggestions are welcome.
Checkout my other blog post series