Comments on: JavaScript values null and undefined http://pierrespring.com/2010/04/25/javascript-null-undefine/ geek, punk, pussy … Sat, 05 Oct 2013 14:25:34 +0000 hourly 1 http://wordpress.org/?v=3.8.12 By: Yoan http://pierrespring.com/2010/04/25/javascript-null-undefine/comment-page-1/#comment-9 Mon, 03 May 2010 16:37:21 +0000 http://pierrespring.com/?p=13#comment-9 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]
true
js> a.hasOwnProperty(2)
false
js> a.length
3

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)
true
js> for(var i in a){ print(i) }
0
2

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 this.name === "object" ? this.name : "No named yet"

]]>
By: Chris D http://pierrespring.com/2010/04/25/javascript-null-undefine/comment-page-1/#comment-8 Wed, 28 Apr 2010 20:04:58 +0000 http://pierrespring.com/?p=13#comment-8 Smile… :-)
Way to go. Nice to see you working on this blog. Keep up the good work!

]]>
By: Pierre Spring http://pierrespring.com/2010/04/25/javascript-null-undefine/comment-page-1/#comment-7 Tue, 27 Apr 2010 11:28:22 +0000 http://pierrespring.com/?p=13#comment-7 @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?
children.splice(2,1);

@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 http://pierrespring.com/2010/04/25/javascript-null-undefine/comment-page-1/#comment-6 Mon, 26 Apr 2010 20:17:45 +0000 http://pierrespring.com/?p=13#comment-6 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 this.name || 'Not named yet';
]]>
By: Yoan http://pierrespring.com/2010/04/25/javascript-null-undefine/comment-page-1/#comment-5 Mon, 26 Apr 2010 20:06:40 +0000 http://pierrespring.com/?p=13#comment-5 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: https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Operators/Special_Operators/in_Operator#Using_in_with_deleted_or_undefined_properties

Food for thought regarding the last example above: https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Operators/Special_Operators/delete_Operator#Deleting_array_elements

]]>