In JavaScript Why do we use "use strict"?

JavascriptFront End TechnologyObject Oriented Programming

Strict Mode is a feature introduced in ES5 that allows you to place a program, or a function, in a “strict” mode.

This strict context prevents certain actions from being taken and throws more exceptions (generally providing the user with more information). Some specific features of strict mode −

  • Variables not declared but being assigned directly will fail. An attempt to assign foo = "bar"; where ‘foo’ hasn’t been defined will fail.

  • You cannot use eval in strict mode

  • You cannot reassign arguments array inside a function

  • Use of with statements is not allowed

You can use your scripts in strict mode as follows −

Add the following at the top of a script to enable it for the whole script −

"use strict";

If you want to use it only within a function then add it only in that context.

function strictFunc() {
   "use strict";
   // rest of function
}
raja
Published on 16-Sep-2019 07:52:28
Advertisements