DEV Community

loading...
Cover image for "use strict" in JavaScript

Use Strict JavaScript "use strict" in JavaScript

rahxuls profile image Rahul ・3 min read

Strict mode makes your program or function follow a strict operating context. Now here is my latest post related to the use strict in JavaScript. This post is very much beneficial for beginners and refreshers.


What is "use strict" ?

=> Strict mode makes your program or function follow a strict operating context.

So what does this actually mean?

=> Now, the compiler throws some silent errors that were previously not thrown or ignored. Also, doesn't allow you to do certain things. Let's see what things.

What does strict mode exactly do ?

The variable will not get added to the global/window object if it is not declared.

designer = "Creativity is everything"

var developer = "Docs are life"

console.log(designer)
//  Creativity is everything
Enter fullscreen mode Exit fullscreen mode

By default, if an undeclared is defined variable is defined, it gets added to the global/window object. This can create an error an may even be hard to find.

To avoid such scenarios, we can declare use strict. Strict mode does not let the use of variables which have not been declared.

var designer = "Creativity is everything"

// YOU PROBABLY MEANT THIS.
Enter fullscreen mode Exit fullscreen mode

Under Strict Mode

'use strict'

designer = "Creativity is everything"
var developer = "Docs are life"

console.log(designer)
// ReferenceError: designer is not defined
Enter fullscreen mode Exit fullscreen mode

Function with duplicate named parameters in non strict mode

In normal JavaScript, we can repeat argument names in functions. The late occurrence of this arguments will override the previous ones.

function logItems( y, y ) {
       console.log(y)
       console.log(y)
}
logItems(44,22)
// 22
// 22
Enter fullscreen mode Exit fullscreen mode

Function with duplicate named parameters in strict mode

The function will not be established if it has same names parameters.

"use strict"

function logItems( y, y ) {
       console.log(y)
       console.log(y)
}

// SyntaxError: Duplicate
// parameter name not
// allowed in this context
Enter fullscreen mode Exit fullscreen mode

Delete operator under strict mode

It stops one from deleting function, variables and function parameters.

1

"use strict"

const x= 4;
delete x

// SyntaxError: Delete of an
// unqualified identifier in
// strict mode. 
Enter fullscreen mode Exit fullscreen mode

2

"use strict"

function LOG() {
   console.log("Log")
}

delete LOG

// SyntaxError: Delete
// of an unqualified
// identifier in strict mode. 
Enter fullscreen mode Exit fullscreen mode

The delete operator in itself is used to remove a property on an object not variables, functions, etc.


Silent Errors are thrown in strict mode

We'll see two examples...

(First)

var user = {
         name: "Rahul",
         age: "16", 
    }
console.log(user)
// { name: 'Rahul', age: '16' }

Object.defineProperty(user, "gender", {
     value: "Male", 
     enumerable: true, 
     writable: false, 
     configurable: false
})

delete user.gender
// No error in console

console.log(user)
//{ name: 'Rahul', age: '16', gender: 'Male' }

Enter fullscreen mode Exit fullscreen mode

(SECOND)

"user strict"

var user = {
      name: "Rahul", 
      age: "16",
     }
console.log(user)
// //{ name: 'Rahul', age: '16' }


Object.defineProperty(user, "gender", {
     value: "Male", 
     enumerable: true, 
     writable: false, 
     configurable: false
})

delete user.gender
// No error in console

delete user.gender
// TypeError: Cannot delete property 'gender' of #Object

Enter fullscreen mode Exit fullscreen mode

How to check if you are in strict mode ?

"use strict"

var isStrict = (function() { return !this; })();
  console.log(isStrict)

// true
Enter fullscreen mode Exit fullscreen mode
var isStrict = (function() { return !this; })();
  console.log(isStrict)

// false
Enter fullscreen mode Exit fullscreen mode

Need Help

Need help in raising fund to buy a Mechanical Keyboard. This pandemic has affected my family badly so can't ask my DAD for it. Please Help Me.


1.png


Thanks For Reading | ⚡ Happy Learning and Coding

Discussion

pic
Editor guide