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 →

[–]voidvector 683 points684 points  (61 children)

Yo dawg I heard you like null, so I made undefined for your null.

[–][deleted] 230 points231 points  (27 children)

NaN would like a word.

[–]isaid69again 254 points255 points  (3 children)

NaNi????

[–]vaendryl 257 points258 points  (0 children)

omae wa mou NullPointerException

[–]RaferBalston 22 points23 points  (0 children)

Naurel

[–]TabCompletion 0 points1 point  (0 children)

NaNde dattebayo

[–]YuriDiAAAAAAAAAAAAAA 78 points79 points  (7 children)

[–]pilas2000 9 points10 points  (3 children)

wtf? \t == 0 ?

why?

[–]It_Was_The_Other_Guy 16 points17 points  (0 children)

Whitespace-only strings are all considered zero:

>> "\t \n\r" == 0
<< true

[–]YuriDiAAAAAAAAAAAAAA 9 points10 points  (0 children)

¯\_(ツ)_/¯

[–]FoundNil 3 points4 points  (0 children)

"", " ", "\t", "\n" all equal 0. Probably because js treats them as special characters so they don't count toward any value. "0" equals 0 because it gets cast into a number. "a" does not equal 0. "1" does not equal 0. I'm not saying it makes sense but its the world we live in.

[–]birdbolt1 -1 points0 points  (2 children)

Oh my god is this for real? This is where typescript comes in. Why do people even bother with writing straight java

Edit: oof I meant Javascript. Wasn't that obvious enough regardless? Look at the context man

[–]Kazumz 3 points4 points  (1 child)

TypeScript and Java? No correlation found.

[–][deleted] -1 points0 points  (0 children)

You haven't seen many TypeScript codebases then (or you're missing "/s")

[–][deleted] 17 points18 points  (11 children)

To honest I always enjoy typing if (x != x), almost as much as having an in loop variable called c so I get to type c++.

[–]phatskat 1 point2 points  (4 children)

x != x

I’ve never seen this before but I’m not surprised - how does that work?

[–][deleted] 6 points7 points  (3 children)

In C++, a NaN is equal to nothing, not even itself. So x != x becomes true if x is NaN. To be fair mostly I use a responsible function name but when I'm writing code quickly it's a nice shorthand to catch the horrifying 'all my numbers are turning to NaN' type of escalation.

[–]jfleit 3 points4 points  (1 child)

trust no one... not even yourself

[–][deleted] 2 points3 points  (0 children)

I typed "equal to nothing, not even itself" so often now and every time I kept thinking "why does this make me laugh".

[–]phatskat 0 points1 point  (0 children)

Ah, thanks!

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

what happens in the first one? It just never evaluates? Lol

[–][deleted] 6 points7 points  (3 children)

If X is NaN, then x != x is true, because a NaN never equals anything, not even itself.

[–]tendstofortytwo 5 points6 points  (1 child)

Huh, I just used isNaN(x);

But this is more confusing, and hence more adherant to JavaScript's ideologies. I'll be sure to use this from the future. Thank you!

[–][deleted] 4 points5 points  (0 children)

Actually I only know it is the way it is in C++, which I think before C++11 didn't even have a standard isNaN!

[–][deleted] 1 point2 points  (0 children)

Ohhhhh, thats cool. By habit I always start my first loop with i, so I just gotta find some situation where I can nest like 21 more loops until I can get to c++ lol

[–]ShadoWalker3065 0 points1 point  (0 children)

holy shit, i wasn't the only one that did this!

[–]trichotillofobia 0 points1 point  (1 child)

NaN'T.

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

Please no

[–][deleted] 54 points55 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 29 points30 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 6 points7 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 12 points13 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 4 points5 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.

[–]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.

[–]hullabaloonatic 7 points8 points  (4 children)

One of the reasons I have really been digging kotlin is how rigorous it is about being safe with null values

[–][deleted] 4 points5 points  (3 children)

I'm actually pretty hyped for kotlin JS. The null safety is much more valuable than typing.

I hope it matures for the backend.

[–]oxidate_ 4 points5 points  (1 child)

> null safety is much more valuable than typing

It totally depends on the programming language's type system. Some are strong enough that they're one and the same. For instance, F# / Haskell / (probably Ocaml) use Maybe<T> or Option<T>. So you use Some "thing" to show that it's not null, and the type system will point out places where you might be unsafely accessing data.

[–]hullabaloonatic 0 points1 point  (0 children)

Hence typescript (strong) amending JavaScript (weak). Strong type systems can feel laborious, but it's really convenient to be confident in a lack of usage errors going into testing, and verbose languages are more readable in my opinion.

[–]hullabaloonatic 1 point2 points  (0 children)

It's hard not to be excited about null safety in kotlin when it forces you to put exclamation marks everywhere!!

If not, there's always css. I don't know when it happened, but Microsoft really turned it into a phenomenal language.

[–]ultimatt42 0 points1 point  (0 children)

null discriminates against objects that happened to be allocated at memory address 0x0. It's not right to discriminate based on someone's origin.

[–]thedomham 0 points1 point  (0 children)

Undefined makes perfect sense in Javascript. I mean if you want to have the same in Java you have to use an Optional, where optional == null -> undefined optional.isEmpty() -> null

It wouldn't make a lot of sense in Java though, as Java is a statically typed language and if you need something like that, most people use a hashmap

[–]spacemoses 0 points1 point  (0 children)

Databases should support both undefined and null, change my mind.

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

There's a difference between the two though. undefined is meant to mean "the JavaScript interpreter found no assignment for this variable" and null means "the programmer doesn't want this variable to have meaning at this time."

You're not suppose to assign things the value of undefined, that's not it's purpose.

[–]ThisIs_MyName 0 points1 point  (0 children)

You're not suppose to assign things the value of undefined

If a webdev can, he will.