-1

I'm curious why in javascript the following code is returning an error:

null.toString()

where in other languages such as ruby, it renders the "logical" empty string:

nil.to_s # ""

I was curious about the behaviour since:

typeof null == "object"

So I thought it would make sense to have the prototype toString() "attached" to it. It seems typeof null returns object only for legacy reasons: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/null

E_net4
  • 27,810
  • 13
  • 101
  • 139
Jeremie Ges
  • 2,747
  • 3
  • 22
  • 37
  • 4
    javascript isn't ruby. – Daniel A. White Feb 06 '18 at 14:26
  • 5
    Because that's the way the language was designed. The value `null` is not an object reference, so it makes no sense (in JavaScript) to access any properties. – Pointy Feb 06 '18 at 14:27
  • 3
    *Most* languages will throw some sort of error when trying to work with `null` as an object. Ruby really is the outlier here. – deceze Feb 06 '18 at 14:28
  • how else would you disinguish empty string and nothing? – Daniel A. White Feb 06 '18 at 14:29
  • @Daniel `typeof something`…!? `something === ''`…!? That's not really an argument. – deceze Feb 06 '18 at 14:29
  • Your downvotes are stupid guys, that's a real question and I want to know what was the design decision around it. – Jeremie Ges Feb 06 '18 at 14:30
  • 1
    um @deceze - `typeof null == 'object'` – Daniel A. White Feb 06 '18 at 14:30
  • @Daniel Yeah, but `typeof '' == 'string'`… – deceze Feb 06 '18 at 14:31
  • @JeremieGes you will have to ask the designers of the languages. – Daniel A. White Feb 06 '18 at 14:31
  • @deceze well what if you wanted to know if you actually had an object, not null. i'm just making the case that we have null for that case. – Daniel A. White Feb 06 '18 at 14:31
  • 1
    More technically speaking: `nil` in Ruby is an object like any other object, `null` in Javascript is a primitive type and not an object. *Why* that was designed that way we cannot tell you, it just was. We can make arguments for or against those decisions, but in the end it simply *was* and that's that. – deceze Feb 06 '18 at 14:34
  • 1
    @DanielA.White you answer is not constructive at all. This guy is asking about language design and why when you scratch the surface it behaves in a specific way. Giving an example from another language it for us to understand why it could make sense. – Laurent Feb 06 '18 at 15:01

6 Answers6

2

null is a "primitive value" and the .toString() is defined in the JavaScript Object's Prototypes. Therefore this can't work.

https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Object/toString

https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/null

messerbill
  • 5,499
  • 1
  • 27
  • 38
  • I like your answer, but why typeof null returns "object" – Jeremie Ges Feb 06 '18 at 14:34
  • 1
    this is because of the way JavaScript is designed. `The value null is written with a literal: null. null is not an identifier for a property of the global object, like undefined can be. Instead, null expresses a lack of identification, indicating that a variable points to no object.`. Check the second link, there is a good explanation for this behaviour – messerbill Feb 06 '18 at 14:36
  • 1
    typeof null // "object" (not "null" for legacy reasons), okay now it makes sense – Jeremie Ges Feb 06 '18 at 14:39
1

null is a primitive value and cause that you can’t call toString function. Only objects provide toString from prototype. Basically, primitive values don’t have properties.

Why typeof null is object?

You know, this all came about because of rushing in early May 1995, which led to a leak of type tag representation shared by null and object types. But null means "no object", so it didn't raise hackles until it was too late to fix in Netscape 2, and after that we were loath to "fix" it and "break the web". That argument only applies more in degree of web population now. We have other fish to fry. This one was has been swallowed already. Let's not change typeof null for ES4 and work on more vital issues. Reference

Another good resource: typeof UnaryExpression

Ele
  • 33,468
  • 7
  • 37
  • 75
1

Try this:

const stringValue = someObject.someValue ? someObject.someValue.toString() : null;

In case someObject.someValue has a type number | null

pmiranda
  • 7,602
  • 14
  • 72
  • 155
0

We cannot call instance method ToString() on a null reference. How can we call not declared function or property in class/object/reference,etc. It simply show an error property toString does not exist not type null

It dependent on language:

  • Javascript - null.toString() - run time error
  • TypeScript - null.toString() - not allow to declare
  • C# - null.toString() not allow to declare (But in c# you can convert by Convert.ToString(null); and it will return "")
  • More

Edit: why typeof null returns "object"

Since when does null.toString() return [object Null]?

Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234
0

is not an object or an identifier to one. Since .toString() is an object prototype, they are incompatible with each other and will throw an error.

L4TTiCe
  • 20
  • 5
0
  • In JavaScript everything except null and undefined can behave like an object.
  • toString is method.
  • method is function inside object.

So since null can never behave like an object you can't apply any method.

Ankit Sinha
  • 1,630
  • 1
  • 20
  • 19