DEV Community

Cover image for Strict Mode of JavaScript
Muhammad Zubair
Muhammad Zubair

Posted on

Strict Mode of JavaScript

Hey 👋, Guys Welcome.

Today we are deeply learning , that what's the difference between LET , VAR & CONST Keyword in JavaScript.

Let's Dive In ..


You will be surprised to know that JavaScript also have a strict mode. Now What does this strict mode do, This is What Today's article about.

Strict Mode

Strict Mode enables us to write much more secure code than normal. This Mode throws much exceptions than normal, so that we can avoid little mistakes that are often ignored by JavaScript. We Will Be Discussing Many Examples to Explore this Feature.

How to Use

In Order to use this Mode, we have to write these two words before writing JavaScript Code

"use strict" or 'use strict'
Enter fullscreen mode Exit fullscreen mode

Working Environment

This Strict Mode can be used in two ways.

  1. Inside Block
  2. Globally
  • Inside Block, Strict Mode Will Only Work in Between these two curly brackets {}.
  • With Global, Strict Mode Will Work Globally in all our Code.

Example

function Mode() {
  "use strict";
  X=20; // X is not defined
}

Z=20;
console.log(Z) // 20
Mode()
Enter fullscreen mode Exit fullscreen mode

In Above Example, We can see that due to strict mode in function, we get an Error. Because Strict Mode in only Enabled inside Function (Mode() Function). According to Exception thrown by strict mode "X is not defined". This exception is due to not defining variable X. We Must Have to use let const or var keyword to Get Rid of this Exception. See Given Example

Example

function Mode() {
  "use strict";
  let X=20;

  console.log(X) // 20
}

Z=30;
console.log(Z) // 30
Mode()
Enter fullscreen mode Exit fullscreen mode

Lets See Few More Examples

Example

'use strict'

function Func(P1,P1) // Duplicate parameter name not allowed in this context
{
  return P1+P1 
}

console.log(Func(3,5)) // !! Output Will Be Wrong

// Always Try to Keep Arguments Name Different
Enter fullscreen mode Exit fullscreen mode

Example

'use strict'

let X=010; // We Cannot Use Leading Zero before Number, Should Be 10 Only 

console.log(X) // Octal literals are not allowed in strict mode.
Enter fullscreen mode Exit fullscreen mode

Example

'use strict'

let eval=20; // Error
let static=10; // Error
let private=10 // Error
Enter fullscreen mode Exit fullscreen mode

We Cannot use Reserved Word as Variable in JavasSript if Strict Mode Enabled.

List of Few Reserved Words is

  • implements
  • interface
  • let
  • package
  • private
  • protected
  • public
  • static
  • yield

As i already mentioned , strict mode helps to write more secure code. JavaScript make it easier for developer code. but strict mode converts previously accepted bad syntax code into error. I think it's good practice to code in Strict Mode.

This is it For Today's Lesson. Hope you liked my article 💛. I will meet you in the next post with something new to learn.

Happy Coding !! âš¡

Top comments (0)