Register  |  Login




Advertisement

Start Your Own Q&A Site

Create your own Q&A site easily, allowing you to quickly grow a new community around any subject matter or generate new organic traffic for your existing website.

Question

Status: Closed Points: 75 Time: 10:19 - Dec 27, 2006  

jgivoni

How do I unset a variable, array element or object property in JavaScript?

I've tried

x = undefined;
x = null;
and
unset(x);

but the two first just assigns the string representation of the reserved words, which are accordingly "undefined" and "null"... - and the last one is just not defined.

So what is the correct way to unset / undefine / remove a simple variable, an array element or an object property so that it will definately not be known later?

Categories

Answer Discussion
Tutorials

 

rcastagna

Date:: Dec 28, 2006

Time:: 10:37

jgivoni,

I haven't found a way to declaratively remove a variable as you're describing. When it comes to javascript, I try to stick with local variables so I don't get "unexpected" results if I want to use the same variable name in a different scope.

It may just require you to change the scope of the variables to prevent the problems you're having.

Ric

jgivoni

Date:: Dec 28, 2006

Time:: 10:55

No, that's actually not my problem.
For instance, I am looping through an array with named elements (properties...?) and doing something to each of them.
I want to remove some elements from it before doing that.

But luckily I found the answer myself:

delete

You can:
delete x // Removes the variable x
delete x[i] // Removes the element with index i from array x
delete x.p // Removes the property p from object / array x

Quite simple - and works in IE and FF - but so strange that so relatively few seems to know of this.

rcastagna

Date:: Jan 04, 2007

Time:: 15:25

Makes sense...I guess I didn't quite understand what you were trying to accomplish from your original submission.

I'm glad you found a workable answer!

Ric

jgivoni

Date:: Jan 09, 2007

Time:: 14:49

Thanks for the input anyway :-)

I have to correct my previous answer though, as the transition from theory to praxis once again proved quirky.
I tried to remove a variable or object with the 'delete x' statement, but nothing happened. I didn't get a warning nor error message and the variable continued to exist with the same value!

It seems to work with properties and array elements though. The deleted property / element will have the value 'null' and 'undefined' if tested afterwards. (Meaning that both 'x.y == null' and 'x.y === undefined' evaluates to true after 'delete x.y')

Jakob

janturon

Date:: Nov 20, 2007

Time:: 03:01

you can delete only implicitly declared variables, i.e.

var x=1; delete x; //false
y=2; delete y; // true

see http://developer.mozilla.org/en/docs/Cor...

however, length property of array is not affected, i.e:
color= new Array("red","green","blue");
delete color[2]; //"green" is deleted and no longer exists in color
alert(color.length); // !! 3 !!

jgivoni

Date:: Nov 20, 2007

Time:: 05:41

Thanks, Janturon, that was quite helpful.

quisquomon

Date:: Dec 11, 2008

Time:: 16:02

color = new Array("red", "green", "blue");
color.splice(1, 2); // deletes 2 elements from index 1 (leaving "red" in the array)

Question Answered

This question has been closed, and points have been rewarded to the following experts:


jgivoni: 75

You're welcome however to comment or give additional information or if you wish, you have the ability to write a Tutorial in the Tutorial Area.

Answer this Question

New User

Email:

Upon submission of this form, you will automatically be registered as a Quomon user and we will send your login information to this address

Registered User

Username:

Password:

Forgot Your Password?

jgivoni

Date:: Jan 14, 2009

Time:: 08:28

To unset a variable, array element or object property, use the "delete" keyword.

delete x[i] // Removes the element with index i from array x
delete x.p // Removes the property p from object / array x

var x=1; delete x; // Doesn't work
y=2; delete y; // Works

However, be careful when testing the lenght property of an array after deleting an element this way; i.e:

color = new Array("red","green","blue");
delete color[2]; // "green" is deleted and no longer exists in color
alert(color.length); // Still reports 3!!

Thanks,
Jakob


Click here to see the Answer Discussion that preceded this tutorial.


Login to rate this tutorial: Good  |  Bad

ealexs

Date:: May 22, 2009

Time:: 04:16

use splice for arrays and delete for associative arrays

var x = new Array();
....
x.splice(....

Regards,
Alex


Click here to see the Answer Discussion that preceded this tutorial.


Login to rate this tutorial: Good  |  Bad

prbr

Date:: Jul 29, 2009

Time:: 14:23

simple:

var x = new Array();
x[0]="text0";
//x[1], x[2]...
x[100]="text100";

x="";

alert(x[100]);

Result-> undefined


Click here to see the Answer Discussion that preceded this tutorial.


Login to rate this tutorial: Good  |  Bad

naseef

Date:: Sep 25, 2009

Time:: 13:51


Click here to see the Answer Discussion that preceded this tutorial.


Login to rate this tutorial: Good  |  Bad

naseef1

Date:: Sep 25, 2009

Time:: 13:59

/**
* This variable provides way to verify any variable is undefined. The easiest
* way to do that is:
*
*  if (typeof(varname) == CONSTANT_UNDEFINED_DO_NOT_SET) {
*   alert('varname is undefined');
*  }
*
*
* NOTE: Do not pass variable to function, as undefined are simply not sent to
* functions.
*/
var CONSTANT_UNDEFINED_DO_NOT_SET;

/**
* Convenient method that can be called to verify the constant that is meant to
* be used verifying variables are undefined is, in fact, undefined. It is
* possible a Javascript can set this, so it becomes undefined.
*
* NOTE: this function will delete this variable so it is undefined again.
*
* @return
*/
function checkUndefinedConstant() {
 var result = typeof(CONSTANT_UNDEFINED_DO_NOT_SET) == 'undefined';
 if (!result) {
  var newUndefinedVarForSetting;
  CONSTANT_UNDEFINED_DO_NOT_SET = newUndefinedVarForSetting;
 }
 return result;
}


Click here to see the Answer Discussion that preceded this tutorial.


Login to rate this tutorial: Good  |  Bad

Answer this Question

New User

Email:

Upon submission of this form, you will automatically be registered as a Quomon user and we will send your login information to this address

Registered User

Username:

Password:

Forgot Your Password?

Ask a Question

Have a new question? Ask!

You have 100 characters to use



Top Experts

View More

Rank

Expert

Points

1.

nidhi

10279

2.

oracleofDelphi

6493

3.

rcastagna

5596

4.

LAGM

4848

5.

PeterNZ

3487

6.

gonzalo

2840

7.

Mason

2770

8.

jgivoni

2303

9.

xarcus

1820

10.

Anpanman

917

Become an Expert

Register today to share your knowledge with the community and be recognized and rewarded for your contributions.


Register Here




"Psst, Quomon is a great site. Pass it on."     Tell a Friend  |   Link To Us  |   Save to Delicious  |   Digg! Digg it



Language Options

English:

www.quomon.com

Español:

www.quomon.es