“use strict” in JavaScript

Tram Ho

When referencing someone else’s js code, we often see the phrase "use strict;" as the above code. So "use strict;" What is it and why is it needed ?

Okay, first we need to know when we declare the above cluster, js will run in strict mode . In this mode, you will not be able to use undeclared variables .

All modern browsers support, except Internet Explorer 9 and lower versions


To use it, we need to indicate "use strict"; at the beginning of js file or before 1 funtion.

For example

Other examples:

When declared inside a function, it is treated as a local scope (only affecting variables inside the function). For example:

Why is strict mode needed?

When using this mode, your js code will be more logically secured

Above, I have sent you an overview of use strict , in addition, you can refer to some cases of using it at w3schools .

Share the news now

Source : Viblo