Sign in
Log inSign up
JavaScript Strict Mode Explained

JavaScript Strict Mode Explained

Kaarthik Sekar's photo
Kaarthik Sekar
·Jul 15, 2021·

2 min read

Strict mode makes it easier to write "secure" JavaScript. Strict mode changes previously accepted "bad syntax" into real errors. In normal JavaScript, mistyping a variable name creates a new global variable. In strict mode, this will throw an error, making it impossible to accidentally create a global variable.

The directive looks like a string, "use strict" or 'use strict'. It is either located at the topmost of a script or the top of a function.

'use Strict';

If we have any code before this then strict mode will not be activated. We can use strict mode even for specific functions.

'use strict';
// This will throw an error cause interface is a reserved word
const interface = 'audio';

// This will throw an error cause we haven't declared x in anywhere
x = 43;

Error Message :

Error

It usually throws an error when we make any mistake ie: using reserved words, using variables that we never declared in our code.


Conclusion

It is a good practice to " use strict " mode at the top of a program (script) or the top of a function. In future articles, the line "use strict" will be skipped.

Hope you guys understood the above strict mode 🙌🏼

What is your thought about strict mode ? Comment it below 👨🏻‍💻

Drop your support 👇🏻