News for hackers. Stuff that matters.
Re:Hello, I’m Mr. Null. My Name Makes Me Invisible to Computers by throwanem on Sunday September 04, 2016 @09:30PM (#12427592)
If you want 'next-level horrible', open your browser's console and take a look at what you get back from "typeof null".
Re:Hello, I’m Mr. Null. My Name Makes Me Invisible to Computers by valleyer on Sunday September 04, 2016 @10:32PM (#12427777)
JavaScriptCore:

>>> typeof null

object

>>> typeof "null"

string

>>> typeof (null + "")

string

Re:Hello, I’m Mr. Null. My Name Makes Me Invisible to Computers by menssen on Monday September 05, 2016 @02:20AM (#12428479)
Something always bugs me about this criticism:

#1 arguably makes sense. Unless "null" were a "type" (which it isn't), the other possible options for this ("undefined", or "string", "number", "array", etc.) all make less sense.

#2 is expected/correct.

#3 makes sense if you accept that the "+" operator aggressively casts its arguments to strings when they are anything other than two numbers. Arguably that's a bad design, but it's not unexpected or unpredictable.

The one completely unexplainable "+" behavior I know of in JS (i.e., a recent node/v8) is the second of these:

  > [] + {}
  '[object Object]' // makes sense because the string representation of [] is ''
  > {} + []
  0 // ???
  > var x = {} + []
  undefined
  > x
  '[object Object]' // but assigning to a variable first makes sense again
Re:Hello, I’m Mr. Null. My Name Makes Me Invisible to Computers by masklinn on Monday September 05, 2016 @03:51AM (#12428769)
> #1 arguably makes sense. Unless "null" were a "type" (which it isn't), the other possible options for this ("undefined", or "string", "number", "array", etc.) all make less sense.

`typeof` gets the primitive type of a value, and exactly like undefined null is a primitive value with its own primitive type, as `typeof undefined` returns `"undefined"` `typeof null` was supposed to return `"null"`, it doesn't due to a bug in the original implementation which was then spec-enshrined as fixing it would break too much stuff: http://www.2ality.com/2013/10/typeof-null.html

Re:Hello, I’m Mr. Null. My Name Makes Me Invisible to Computers by codepsycho on Monday September 05, 2016 @03:54AM (#12428776)
This is because:

> [] + {}

Becomes:

> [].toString() + {}.toString()

Empty array to a string is "" (empty string). Object to string is "[object Object]".

> {} + []

Becomes:

> +[]

The prefix turns the array into a number, same as Number([]). This seems to be because {} is interpreted as a code block, not an object, due to it being the first thing.

Put it in parentheses and it'll do toString like before.

Re:Hello, I’m Mr. Null. My Name Makes Me Invisible to Computers by BerislavLopac on Monday September 05, 2016 @05:38AM (#12429152)
And now the obligatory WAT video: https://www.destroyallsoftware.com/talks/wat
Re:Hello, I’m Mr. Null. My Name Makes Me Invisible to Computers by throwanem on Monday September 05, 2016 @03:55AM (#12428782)
The first of which is just my point. Null cannot be treated safely as an object in any way. To attempt such elicits an exception. Yet its type is 'object' nonetheless, needlessly complicating every type check that has to do with objects. I've been working with Javascript, professionally and as a hobbyist, since about 1995, and have yet to encounter any satisfactory reason why this should be the case.

Edit: And a comment nearby explains that it is the way it is for the same reason Make barfs on leading spaces. Well, at least that's a reason! A terrible, terrible reason.