Sign in to follow this  

[web] Odd Javascript Bug

This topic is 2632 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,

I have the following snippet of javascript code which is acting in a rather odd way


var firstNewLine = content.indexOf('\n');
if(firstNewLine == -1)
{
return {description : content};
}

var typeString = content.substring(0, firstNewLine);
typeString = typeString.toLowerCase()

//other stuff

if(imageDict[typeString] == undefined)
{
//other stuff
}


In a particular run typeString ends up getting set to "bar" when I then index imageDict using this I get undefined. This is odd as when using the Chrome debugger if I place a breakpoint on the second if statement it shows that imageDict does indeed have a defined entry for "bar". If I put imageDict["bar"] into the watch expressions then I get the result I expect (A path to an image file) however if I add a watch expression for imageDict[typeString] I get undefined. So when the code actually runs the expression in the second if statement evaluates to true.

So it appears I have two strings "bar" and "bar" which are visually identical in their representation in the chrome debugger but javascript considers different. Can anyone shed any light on what might be happening?

Note that this code works fine in other instances, it's just this one particular case.

Share this post


Link to post
Share on other sites
I don't know how to solve your problem, but here are a few general comments anyway:

First, checking for undefined goes like this:

if(typeof imageDict[typeString] === 'undefined')


Second, you could just use the in operator:

if(typeString in imageDict)

Share this post


Link to post
Share on other sites
Quote:
Original post by Konfusius...checking for undefined goes like this:

if(typeof imageDict[typeString] === 'undefined')
...


Imho the best way to check against undefined in javascript is to declare an undefined variable in your current scope and check against that.


var undefined; // declare a variable called undefined and ommit the assignment
...
if (xyz === undefined) // now check against that variable, which in turn is undefined
...

Share this post


Link to post
Share on other sites
Turns out it was an extra '\r' character, something I really should have been able to work out on my own [grin], thanks smr.

Konfusius: Thanks for the tips, out of interest why do you think the typeof method is better than my current method of checking for undefined (Ignoring for now that I should be using the in operator for this anyway)? Is it because undefined isn't actually a keyword but javascript interprets it as a variable that I haven't defined and thus has the type undefined so appears to act like an 'undefined' keyword would in this instance should it exist?

Share this post


Link to post
Share on other sites
Quote:
Original post by Monder...Konfusius:...Is it because undefined isn't actually a keyword but javascript interprets it as a variable that I haven't defined and thus has the type undefined so appears to act like an 'undefined' keyword would in this instance should it exist?...


I'm not Konfusius, but let me answer instead ;-)

You're right, undefined isn't a keyword in javascript. It is a global property to which no value (or to be correct: the value undefined) was assigned. That's why it is undefined. And that's why you can redeclare it in your local scope (like i did in my example above) or even assign another value to it.

But for Firefox 4 and above the global property undefined is read only and cannot be redefined anymore:
undefined at Mozilla developer center

With the use of typeof you need a string comparison, so using the equality operator === with an undefined property should give you better performance when doing lot's of undefined checks in a loop.

Share this post


Link to post
Share on other sites
There is no 'undefined' constant in javascript, only the type. (Edit: Now there is. Awesome. :))

A problem arises when you just compare a value to undefined non-strictly, the comparison null == undefined also evaluates to true.

The solution by Anntor works as well, but using typeof the_variable === 'undefined' is the most practical (because you don't have to define that 'undefined' variable, and you can't overwrite it) and prevalent way.

Share this post


Link to post
Share on other sites

This topic is 2632 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this