Strict Mode Flashcards
how do you use ‘strict mode’?
at the top of the file:
“use strict”;
How does strict mode make code more stable?
Strict mode prohibits using names and syntax that may conflict with future versions of JavaScript.
How does strict mode prevent slow code?
Strict mode prevents some code that can inhibit JavaScript’s ability to optimize a program so that it runs faster.
How does strict mode help eliminate bugs?
Strict mode eliminates somesilent errorsthat occur in sloppy mode by changing them to throw errors instead. Silent errors occur when a program does something that is unintended, but continues to run as though nothing is wrong. This can lead to incorrect results or errors much later in execution that are subsequently difficult to track down.
how is strict mode scoped?
Strict mode is lexically scoped; that is, it only applies to the code that enables it.
function foo() {
“use strict”;
// All code here runs in strict mode
}
function bar() {
// All code here runs in sloppy mode
foo(); // This invocation is sloppy mode, but foo
runs in strict mode
// All code here runs in sloppy mode
}
What does strict mode do?
- Strict mode does not let you create variables without explicitly declaring them
- In strict mode, using function call syntax on a method sets
this
set toundefined
- If you forget to use
this
when tryin to assign to an object property, strict mode will throw an error rather than add that property to the global object. - raises an error if a number starts with 0 rather than converting to octal number
- prevents declaring two function parameters with the same name.
- prevents using some newer reserved keywords, such as
let
andstatic
, as variable names.