Stringy Objects
Article Index
Stringy Objects
Solution

A Programmer's Puzzle in which we contemplate situations in which string equivalence in Javascript is clearly not what it seems - and explain why it all goes wrong.

 

Banner

Background

Javascript makes it easy to add functions that manipulate basic data. It is also true that in Javascript everything is an object.

For example, the humble and over-worked string literal is an object complete with methods and properties. For example:

alert("ABCD".length);

displays 4. This almost seems against the laws of programming when you first see it but yes raw data in the form of a quoted string is an example of a string object and has the methods and properties of a string object.

Banner

Puzzle

With this in mind consider the following simple function that tests to see if a string is equal to a target string literal:

function answer(s){
return s==="yes";
};

Notice that this is an exact test. That is, s has to be equal to "Yes" and not convertible to "Yes" from some other data type. In this case using either == which is a looser test for equality, or using === which is strict equality doesn't seem to be a big issue. After all a string is either a string or it isn't and it is either equal to "Yes" or it isn't. So you could use:

function answer(s){
return s=="yes";
};

just as easily and expect to get the same results. But it it is generally held (for example JSLint) that you should avoid == and != so the first function is the one that was used.

The utility function was used as part of a much larger program and eventually a bug was tracked down to it sometimes returning false when the string passed to it was indeed "yes".

The function was carefully checked and two debugging commands added:

function answer(s){
alert(s);
alert(s==="yes");
return s ==="yes";
}

so that the value of s could be seen along with the result. During one run the function returned:

yes

and

false

That is, the parameter passed in stored "yes" and  the result of the comparison s==="yes" was false.

This cannot be!

What is going on?

Turn to the next page when you are ready to find out.

 javascriptpuz

<ASIN:1871962579>

<ASIN:1871962560>

<ASIN:1871962501>

<ASIN:1871962528>

<ASIN:1871962625

 

 

Banner