Rahul
RAHULISM

RAHULISM

"use strict" in JavaScript

"use strict" in JavaScript

Rahul
·Nov 29, 2020·

5 min read

Subscribe to my newsletter and never miss my upcoming articles

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

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.

Under Strict Mode

'use strict'

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

console.log(designer)
// ReferenceError: designer is not defined

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

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

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.

2

"use strict"

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

delete LOG

// SyntaxError: Delete
// of an unqualified
// identifier in strict 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' }

(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

How to check if you are in strict mode ?

"use strict"

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

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

// false


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

Did you find this article valuable?

Support Rahul by becoming a sponsor. Any amount is appreciated!

See recent sponsors Learn more about Hashnode Sponsors
 
Share this