The “use strict” directive is used to enable strict mode, which is a way of writing JavaScript code that helps to prevent errors and improve safety. It enforces stricter rules for code, which can help to prevent certain types of errors.
For example, consider the following code:
function myFunction() {
x = 10;
}
myFunction();
console.log(x); // prints 10
Without strict mode, this code would execute without any errors. However, when using strict mode, the code will throw an error because the variable x was not declared before it was used.
“use strict”;
function myFunction() {
x = 10; // throws an error
}
myFunction();