Fixing "Delete of an unqualified identifier" Errors in JS
The "Uncaught SyntaxError: Delete of an unqualified identifier in strict mode." error occurs in JavaScript, whenever you try to use the delete
keyword on a variable in strict mode. In order to fix the error, avoid using the delete
keyword on variables.
'use strict'
const cloud = 'βοΈ'
// β This will throw "Uncaught SyntaxError: Delete of an unqualified identifier in strict mode."
delete cloud
Strict mode in JavaScript eliminates silent errors, prohibits certain syntax, and fixes mistakes that make it difficult for JS engines to perform optimizations.
In the above code example, we are using strict mode, and we try to delete a variable using the delete
keyword. In non-strict mode, this operation will only return false
. But in strict mode, we will get the following error:
Uncaught SyntaxError: Delete of an unqualified identifier in strict mode.
Why is this error happening?
The delete
operator in JavaScript cannot be used on variables. It is not allowed in strict mode, therefore you will get the above error. The delete
operator can only be used on the properties of an object. Object properties are "qualified identifiers", therefore if you use the delete
operator on them, you won't get an error.
'use strict'
const cloud = {
emoji: 'βοΈ'
}
// βοΈ cloud.emoji is now undefined
delete cloud.emoji
Moreover, the delete operator cannot be used to free up memory. In case you would like to free up the value of a variable, you can simply assign it to null
:
'use strict'
let cloud = 'βοΈ'
// βοΈ Correct way to free up a variable
cloud = null
Notice that the variable has been changed to let
. You cannot reassign values to a const
, otherwise, you will get another error: "Uncaught TypeError: Assignment to constant variable."
Rocket Launch Your Career
Speed up your learning progress with our mentorship program. Join as a mentee to unlock the full potential of Webtips and get a personalized learning experience by experts to master the following frontend technologies: