"use strict" in JavaScript

Have you ever come across this line "use strict" written on top of the code and wondered what this does and what is the use of this line?

Only by writing this one single line on the top of the code will allow us to write better programs and also make less mistakes in our code. Let us now understand how will that be possible.

JavaScript is a very flexible language and often it allows us to get away with some of the mistakes or bad syntaxes without giving any sort of warning or error messages. But by using strict mode, the bad syntaxes are converted into errors which help in reducing the amount of unexpected code behaviors and leading to a more secure code.

Applying strict mode

  • Strict mode can be applied to the entire file by writing "use strict" on top of the file
"use strict"
const username="John"
  • Strict mode can be applied on individual functions by writing "use strict" in the first line of the function block.
function user {
"use strict"
const username="John";
}

But strict mode cannot be applied to block statements. For example, in the below code snippet, strict mode will not be applied within this block of code.

if (true){
    "use strict"
    a=23;
    console.log(a) // output: 23 use strict not applied here
}

Let us now look at some examples where using the strict mode can help us write more efficient code and avoid unexpected errors

  • In strict mode we cannot have undeclared variables. Variables can be assigned only after they are declared using the let, const or var keywords. But when strict mode is not used, and if any variable assignment is done without declaring them first, by default a new global variable is created. This might cause an issue if accidently someone misspells the already declared variable which might lead to the creation of a new global variable.
"use strict"
username ="June"    //  this throws a ReferenceError: username is not defined

In the above example, we have to first declare the variable and then assign the value to it. This code can be fixed by declaring the username and then assigning value to it as displayed below.

"use strict"
const username="June"
  • In strict mode, we cannot assign values to read only properties. In normal JavaScript, when we assign values to read only properties, we do not get any warning messages or errors. But while using strict mode, we get an error when we try to assign values to read only properties, non writable properties or non existing objects / variables.
"use strict"
const undefined=23   // this throws a SyntaxError: Identifier 'undefined' has already been declared

In the above code, when we try to assign value to the reserved keyword undefined, we get a syntax error. Where as if we do not use the strict mode, the above assignment will not give any warning or error message.

  • In strict mode, deleting a variable is not allowed. This will help us from accidently deleting any variables from the program. Variables can be deleted from the programs not using strict mode.
"use strict"
const a=21
delete a         // throws a SyntaxError: Delete of an unqualified identifier in strict mode
  • In strict mode, using eval will not introduce new variables in the surrounding scope. Eval only creates variables for the code being evaluated for strict mode. In normal code, eval introduces that variable in the surrounding / global scope.
"use strict"
eval("var x")     // this x will not be created as a separate variable for the global scope
  • Duplicate function parameter names are not allowed in strict mode. In normal code when we have duplicate function parameter names, the last duplicated argument hides the previous value for that same parameter name. But in strict mode, when there are duplicate parameter names, they give syntax errors.
"use strict"
function example (a,b,b,c){    // this gives a SyntaxError: Duplicate parameter name not allowed in this context
return [a,b,c]                            
}
console.log(example(22,24,32,55))
//  without using strict mode

function example (a,b,b,c){ 
return [a,b,c]
}
console.log(example(22,24,32,55))   // output: [22, 32, 55]

Here we have seen some of the examples where strict mode can be used to avoid unexpected behaviors and write more secure code. Using strict mode has a lot of security advantages and also it allows developers to write a better code.

Happy Coding !!