Notes

strict mode

Edit on GitHub

JS Interview Prep
2 minutes

What does use strict do?

Strict mode allows you to run the program in a strict operating context. It makes debugging easier. Code errors that would have otherwise been ignored or would have failed silently would now throw errors or exceptions.

Strict mode throws more errors and disables some features in an effort to make yor code better.

1// It is a string
2// goes on top of the file
3"use strict"

It is a string value, newer browsers that supported it made a strnig value insetead of use keyword to let older browsers not fail and just ignore it as a string if they don’t support it.

1// strict mode can also be used in parts of your code  
2// instead of the entire file 
3function myCode() {
4	"use strict"
5	// Strict mode is ON..
6} 

Examples:

  • Using a variable before it is defined will cause errors. Assigning a value to an undeclared variable will give an error.
1let number = 10
2numbr = 11 // silly typo
3console.info(number)
1// srict mode (REFERENCE error)
2ReferenceError: numbr is not defined
1// not strict mode (SYNTAX error)
2SyntaxError: Identifier 'number' has already been declared
  • Stops you from using keywords reserved for future version of JavaScript (used to error out on let when ES2017 wasn’t supported)
  • You can’t delete functions or variables or arguments to functions in strict mode
  • It disables features that are confusing or poorly thought out
  • It catches some common coding bloopers, throwing exceptions.
  • It prevents, or throws errors, when relatively “unsafe” actions are taken (such as gaining access to the global object).
  • In strict mode you can’t assign values to variables that haven’t been previously declared