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…


This content originally appeared on DEV Community and was authored by Aravind Sanjeev

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 = 10

random = 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


This content originally appeared on DEV Community and was authored by Aravind Sanjeev


Print Share Comment Cite Upload Translate Updates
APA

Aravind Sanjeev | Sciencx (2021-09-28T18:47:05+00:00) What is Use Strict? Explained. Retrieved from https://www.scien.cx/2021/09/28/what-is-use-strict-explained/

MLA
" » What is Use Strict? Explained." Aravind Sanjeev | Sciencx - Tuesday September 28, 2021, https://www.scien.cx/2021/09/28/what-is-use-strict-explained/
HARVARD
Aravind Sanjeev | Sciencx Tuesday September 28, 2021 » What is Use Strict? Explained., viewed ,<https://www.scien.cx/2021/09/28/what-is-use-strict-explained/>
VANCOUVER
Aravind Sanjeev | Sciencx - » What is Use Strict? Explained. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/09/28/what-is-use-strict-explained/
CHICAGO
" » What is Use Strict? Explained." Aravind Sanjeev | Sciencx - Accessed . https://www.scien.cx/2021/09/28/what-is-use-strict-explained/
IEEE
" » What is Use Strict? Explained." Aravind Sanjeev | Sciencx [Online]. Available: https://www.scien.cx/2021/09/28/what-is-use-strict-explained/. [Accessed: ]
rf:citation
» What is Use Strict? Explained | Aravind Sanjeev | Sciencx | https://www.scien.cx/2021/09/28/what-is-use-strict-explained/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.