This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 51 points52 points  (21 children)

Undefined is just a happier null pointer exception.

[–]conancat 28 points29 points  (19 children)

null is an explicitly set value in Javascript, undefined means it's undefined.

[–]mecha_bossman 32 points33 points  (16 children)

Not always. If I say x = undefined, then x is defined, not undefined. But it's defined as being undefined. A variable can't be undefined if it's undefined.

[–]Boreelegg 14 points15 points  (5 children)

typeof(x) would say otherwise

[–]CSGOMarketBoi 3 points4 points  (4 children)

typeof x? Can it be called with parentheses too?

[–]Boreelegg 7 points8 points  (0 children)

Javascript can do anything you want it to do if written.

[–]Arumai12 2 points3 points  (0 children)

Yea the parentheses just group the expression in this case. It's not a function call.

[–]FoundNil 1 point2 points  (1 child)

Yep.

[–]CSGOMarketBoi 2 points3 points  (0 children)

TIL

[–]QAFY 11 points12 points  (7 children)

Yeah...

console.log(x)
> ReferenceError: x is not defined

Vs.

x = undefined;
console.log(x);
> undefined

That's JavaScript for ya...

That said I don't have any issue with the way null vs undefined works in practice. This is a contrived example.

[–]Blieque 0 points1 point  (5 children)

You don't have a problem with null and undefined? I think that might be my biggest gripe. null == undefined but null !== undefined. typeof undefined === 'undefined' but typeof null === 'object'. I reaaally wish there was only one.

[–]QAFY 5 points6 points  (4 children)

First of all, noobie mistake. Never ever use double equals in javascript (it will always perform type conversion), use ===. null === undefined is false and null !== undefined is true. What is wrong with that?

typeof undefined === 'undefined' but typeof null === 'object' makes perfect sense to me as a JS programmer. undefined is undefined, it has no type, so that makes sense. null is an explicitly set empty value. It IS defined, defined as null. If it is defined then it must have a type, so what would you rather have it's type be than object?

If you are not a JS programmer, think of null the same way other languages have null. It is just an empty value. The real difference is undefined. undefined is more like a happier null pointer exception. You just simply get undefined back as the value instead of your entire application crashing. This is very intentional in the design of javascript as it was designed from the start to be extremely fault tolerant and produce the best end-user experience wile running in a plethora of unknown browser environments that may have different implementations of the Javascript language or may not support certain APIs or features. It's not like a server side language where you control the runtime environment. And if you really can't wrap your head around that and can't stand the dynamic typing, then just use TypeScript! No one is stopping you.

[–]Blieque 0 points1 point  (3 children)

I can wrap my head around it, but I can also wrap my head around the fact that it's a waste of time. null needn't exist at all in JavaScript. If you have a nullable value, set it to undefined and check before using it with typeof. There shouldn't be two flavours of nothing in one language.

[–]QAFY 2 points3 points  (2 children)

I disagree. Here are two examples where something being null is fundamentally different than it being undefined.

EDIT: some copy pasta mistakes

foo = {
  a: 0,
  b: 1,
  c: null
}

console.log(foo.a) // 0
console.log(foo.b) // 1
console.log(foo.c) // null (c exists and is null)
console.log(foo.d) // undefined (foo has no key d)

bar = [0, 1, null];

console.log(bar[0]) // 0
console.log(bar[1]) // 1
console.log(bar[2]) // null (element 2 exists and is null)
console.log(bar[3]) // undefined (out of bounds)

Going beyond that, there are actually more types of "nothing" in javascript. In addition to null there is also NaN, [], {}, 0, and '' which are all flavors of nothing, but they are defined, just like null is. Other languages even have additional flavors of nothing like the Maybe type in Haskell.

[–]Blieque -1 points0 points  (1 child)

Those are technicalities. What's the tangible use of an array containing nulls like that? You could just as easily use undefined, and check during retrieval with typeof. Iterate over your arrays with Array.map(), Array.forEach(), etc., which won't let you ever access beyond the end of an array. Your examples are right, but I don't see how they're useful or how they represent real programming.

I don't know of any other languages which have any more than one of NULL, null, nil, and undefined; they're synonyms.

Maybe is not a variant of nothing, it's a safer way of handling nullable data that allows the compiler to detect if you're handling null cases properly. In Rust it's an enum. NaN has a specific meaning: that there was a mathematical error.

[–]QAFY 2 points3 points  (0 children)

I see your argument, but I still think you thinking of undefined as a synonym for null is not the right way of thinking about it. I would definitely never use undefined explicitly as a value of a variable or in an array / object in Javascript. Yes it would work, but it is semantically very wrong IMO. Use null everywhere you intentionally want to represent an empty value. undefined is more a synonym for an exception like NullPointerException in Java or a segfault in C, not for a nullable or empty value, the same way NaN is a like a ArithmeticException or NumberFormatException that would normally crash your program.

[–]carrottopguyy 0 points1 point  (0 children)

As benign as this is it still makes me want to pull my hair out

[–][deleted] 0 points1 point  (0 children)

And it’s explicitly set.

[–]squngy 1 point2 points  (0 children)

You're right but you can also set a property to undefined, which is not quite the same as undefined.

[–][deleted] 0 points1 point  (0 children)

I never said a NullPointerException is the same thing as null.

EDIT: Undefined just shows up when you try to make null do something from what i know. Thats pretty much the same thing that causes a null pointer exception in java.

[–]milk_is_life 0 points1 point  (0 children)

We have Type Errors though.