Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
September 28, 2021 06:47 pm GMT

What is Use Strict? Explained

Read original

Introduction

"use strict" is a directive that is used to enable strict mode in JavaScript. It's simply a string that is placed on top of our code. When the browser comes across the string, it runs the code below in strict mode. In this post, we will look at why strict mode exists and when we should use it.

Strict mode

Strict mode in JavaScript was introducted in ECMAScript 5. Until then, JavaScript has evolved without any compatibility issues. Although that came with the added benefit of never breaking the code, it also means any imperfections had to be kept. So strict mode was introduced to make some breaking changes while allowing already existing code to work.

JavaScript code will work as intended in normal (now dubbed "sloppy") mode. With strict mode, it will be more restrictive giving less margin for error. It will also increase compatibility for future.

Enabling strict mode

Strict mode can be enabled for the entire script or individual function.

For whole scripts

To enable strict mode for the whole script, we provide "use strict" on top.

"use strict"const c = "The entire script now works in strict mode"

If you're mixing strict mode code with non-script mode ones, you could run into some trouble. If the strict mode code is the parent, entire JavaScript will run in strict mode. If the non-strict mode code is the parent, strict mode will be rendered useless. It is not ideal to mix strict mode code with non-strict ones.

For individual function

To use strict mode for an individual function, we put the same statement but inside function body.

function strictmode() {  "use strict"  // function definition}

ECMAScript 2015

ECMAScript 2015, more famously ES6 has introduced modules and classes. All codes related to classes are run in strict mode by default. In modules, anything exported using the export keyword is in strict mode.

function strictmode() {  // function definition  // runs in strict mode}export default strictmode

Same applies for named exports.

export function strictmode() {  // function definition  // runs in strict mode}

Why strict mode?

As already mentioned, strict mode gives a more restrictive environment to developers. An example of this is declaring variables.

While writing normally, we can simply assign a value without declaration.

randomVariable = 10

However, in strict mode this will throw an error.

"use strict"randomVariable = 10// throws a reference error

Hence, we must always declare the varibale with var, let or const. This makes it impossible to accidently create a global variable.

"use strict"var random = "string"const num = 10random = 23 // works because already declared

Strict mode will also throw an error when assigning value to non-writable globals like undefined, NaN, etc.

Strict mode also prohibits duplicate parameters.

function add(a, b, b) {  "use strict"  return a + b + b // throws an error}

In strict mode, each parameters must be unique. These are few of the behavioral changes. You can read a more detailed account of changes in this mdn article.

When to use strict mode?

Ideally anytime you aren't having conflict with your existing code. Since strict mode is meant to keep your code tidy and future-proof, there is no reason to avoid it. However, you must do sufficient feature testing to make sure the browser has implemented it under strict mode. Strict mode is now a decade-old feature and is available in all major browsers


Original Link: https://dev.to/aravsanj/what-is-use-strict-explained-3hc8

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To