JavaScript
< 1 minute read

When to add ‘use strict’ in JavaScript and what implications implies.

If you don’t use 'use strict', the context of this will be the global window object.


function getName() {
    console.log(this)
}

getName()

When using strict mode, the context of this inside a function on the global context will be undefined:


'use strict'

function getName() {
    console.log(this)
}

getName()

Overall, it’s a best practice to use the strict mode in every file (no need to do this is module, since they use strict mode by default) as it reduces the probability to have an unexpected scope.

Rarely, someone will refer to the window object using this.

To use the strict mode inside a function, you need to add 'use strict' inside the function, before any other instruction.