What is Strict mode in JavaScript

Strict mode was introduced by ECMAScript 5 to JavaScript. It is useful to detect silent errors as they would throw an error in this mode. This makes JavaScript debugging easier and helps developers avoid unnecessary mistakes.

How to enable Strict Mode

Strict mode is optional. The strict mode can be enabled by using “use strict” in front of the code where strict mode is required.

‘use strict’

Const name =’Saily”

const hello = () => “hey’

// . . .

What changes in Strict Mode

  • Syntax errors
  • New runtime errors
  • Semantic differences

Following cases will throw an error

  • Octal syntax var n= 023;
  • with statement
  • Using delete on a variable name
  • Using eval or arguments as variable or function argument name
  • Reserved keywords

Following cases will throw an error

  • Setting a value to an undeclared variable
  • Trying to delete a non-configurable property
  • Declaring function in blocks if (a < b){ function f() {} }
  • Some other obvious errors

Strict mode catches some common coding bloopers, throwing exceptions. It prevents or throws errors when relatively “unsafe” actions are taken. It disables features that are confusing or poorly thought out.

Leave a Reply

Your email address will not be published.