Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does String(null) work?

null and undefined don't have a toString or valueOf method. Afaik using String calls the toString method of its parameter (e.g. String({}) => [object Object]).

Why do String(null) or String(undefined work then? It doesn't implicitly do Object.prototype.toString.call(null). because that evaluates to [object Null].

[edit]: from the spec ECMA-262/5th edition (page 48). This doesn't add to clarification, I'd say:

/* Table 13 — ToString Conversions   ------------------------------------------------------------------------- Argument Type  | Result   ------------------------------------------------------------------------- Undefined      | "undefined" Null           | "null"   Boolean        | If the argument is true, then the result is "true". ...            | ... */ 
like image 926
KooiInc Avatar asked Apr 28 '12 09:04

KooiInc


People also ask

What does string null do?

An empty string is a string instance of zero length, whereas a null string has no value at all. An empty string is represented as "" . It is a character sequence of zero characters. A null string is represented by null .

Why use null instead of empty string?

If you were to use s , it would actually have a value of null , because it holds absolute nothing. An empty string, however, is a value - it is a string of no characters. Null is essentially 'nothing' - it's the default 'value' (to use the term loosely) that Java assigns to any Object variable that was not initialized.

What happens if you add null to a string?

If the input object is null, it returns an empty (“”) String, otherwise, it returns the same String: return value == null ? "" : value; However, as we know, String objects are immutable in Java.

Is null a valid value from string?

Short answer: YES, literal string "null" can be a valid property, but it depends on the logic behind your project. Show activity on this post. see this below code it might helpyou. String value = "null"; // where value key work is holding string value which is "null".


2 Answers

After reviewing my previous answer, it seems a complete overhaul of my previous answer is necessary. I was way over complicating it, as the short answer is that these are standards-specified special cases.

The specification for String() (String used as a function):

15.5.1.1 String ( [ value ] )

Returns a String value (not a String object) computed by ToString(value). If value is not supplied, the empty String "" is returned.

The ToString function (that exists internally, not in userland) is defined as follows (9.8):

"The abstract operation ToString converts its argument to a value of type String according to Table 13"

Argument Type | Result Null | "null" Undefined | "undefined" 

This means that String(null) and String(undefined) go into this special table of types and just return the string values valued "null" and "undefined".

A user-land pseudo-implementation looks something like this:

function MyString(val) {     if (arguments.length === 0) {         return "";     } else if (typeof val === "undefined") {         return "undefined";     } else if (val === null) {         return "null";     } else if (typeof val === "boolean") {         return val ? "true" : "false";     } else if (typeof val === "number") {         // super complex rules     } else if (typeof val === "string") {         return val;     } else {         // return MyString(ToPrimitive(val, prefer string))     } } 

(Note that this example ignores the constructor case (new MyString()) and that it uses user-land concepts rather than engine-land.)


I got a bit carried away and found an example implementation (V8 to be specific):

string.js:

// Set the String function and constructor. %SetCode($String, function(x) {   var value = %_ArgumentsLength() == 0 ? '' : TO_STRING_INLINE(x);   if (%_IsConstructCall()) {     %_SetValueOf(this, value);   } else {     return value;   } }); 

macros.py:

macro TO_STRING_INLINE(arg) = (IS_STRING(%IS_VAR(arg)) ? arg : NonStringToString(arg)); 

runtime.js:

function NonStringToString(x) {   if (IS_NUMBER(x)) return %_NumberToString(x);   if (IS_BOOLEAN(x)) return x ? 'true' : 'false';   if (IS_UNDEFINED(x)) return 'undefined';   return (IS_NULL(x)) ? 'null' : %ToString(%DefaultString(x)); } 

The NonStringToString (which is essentially what is of interest), is luckily defined in psuedo-JS-land. As you can see, there is indeed a special case for null/true/false/undefined.

like image 188
Corbin Avatar answered Oct 13 '22 21:10

Corbin


There is probably just some extra checks and handling for special cases like null and undefined.

MDN says:

It's possible to use String as a "safer" toString alternative, as although it still normally calls the underlying toString, it also works for null and undefined.

like image 31
Thilo Avatar answered Oct 13 '22 21:10

Thilo