1

I am using AngularJS 1.6, Can i use use strict with my Controller, Directives, services, etc.

Is it a good practice using 'use strict' with AngularJS app?

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
asder
  • 27
  • 8

2 Answers2

2

use strict generally is a good idea, but there's a bunch of circumstances where it's unnecessary.

For example, if you use a typing system, most typing systems will automatically add use strict. This is true for flow or typescript.

If you use es6 modules, 'use strict' is also enabled by default.

If you use neither, use strict is still a good idea. It definitely doesn't hurt.

Aside: If you care about the security that use strict gives you, you might care about using a typing system like typescript or upgrade your angular version to something much more recent. Although I recognize it's not always an option with large legacy code bases.

Evert
  • 93,428
  • 18
  • 118
  • 189
2

There are many advantage of using 'use strict' like

  1. Eliminates some JavaScript silent errors by changing them to throw errors.
  2. Fixes mistakes that make it difficult for JavaScript engines to perform optimizations: strict mode code can sometimes be made to run faster than identical code that's not strict mode.
  3. Prohibits somesyntax likely to be defined in future versions of ECMAScript.

Read this for more details

deepak thomas
  • 1,118
  • 9
  • 23