Comments on: JavaScript values null and undefined geek, punk, pussy … Sat, 05 Oct 2013 14:25:34 +0000 hourly 1 By: Yoan Mon, 03 May 2010 16:37:21 +0000 Applying `delete` on an array seems very counter-intuitive to me. I’d expect it to behave like (the awesome) splice but it doesn’t.

js> var a = [1,2,3]
js> delete a[1]
js> a.hasOwnProperty(2)
js> a.length

it’s not the same as doing it manually though (even if you say that it makes no sense)

js> a[2] = undefined
js> a.hasOwnProperty(2)
js> for(var i in a){ print(i) }

It’s very very confusing, I think. I’m feeling all PHPish at this point :-)

@michael, @shvi just go with the ternary operator: return typeof === "object" ? : "No named yet"

By: Chris D Wed, 28 Apr 2010 20:04:58 +0000 Smile… :-)
Way to go. Nice to see you working on this blog. Keep up the good work!

By: Pierre Spring Tue, 27 Apr 2010 11:28:22 +0000 @yoan: I had never heared of the in operator before. Yet, for the purpse of this article, which is to understand the difference between null and undefined I think the examples are better off without it.

I read the link about deleting array elements. And my head kind of doesn’t get why someone would do the following:

var children = ['Lou', 'Frank', Lillith];
children[2] = undefined; // makes no sense

children = ['Lou', 'Frank', Lillith];
// is slow, yet the only solution not to
// fuck with the length property and having
// vanilla for loops working as expected. no?

@michael: Thanks for pointing out the shortcut. I’ll edit the post right away! Now, what I meant to say about PHP is that I dislike the fact that it sets an undefined variable to null. Not the fact that it triggers a notice. I like the fact that PHP triggers a notice, yet most people working with PHP do not display them ;)

By: Michael Mon, 26 Apr 2010 20:17:45 +0000 I don’t know if I follow your conclusion. In my opinion accessing a non existing property should very much throw an error, to prevent long debugging sessions when there’s some evil typo in your code. At least that happens to me far more often than that I have a need for the behavior you described.

By the way, to make the second example (with null) less verbose, you can also use the fact that null evaluates to false and use this shortcut:

return || 'Not named yet';
By: Yoan Mon, 26 Apr 2010 20:06:40 +0000 I usually find the “in” operator way nicer to tests the presence of something, or the hasOwnProperty if you’re picky. Once again, you must know what you’re doing at any time:

Food for thought regarding the last example above: