0

Below is what I have in Javascript

var x=["1","2","3","4","5","6"];
c = (b = x[1])[0] ;
alert("B=" + b);
alert("C=" + c);

When I run this in Firefox 9.0.1, I get below output.

B=2
C=2

When I run same in IE7, I get below output.

B=2
C=undefined

Can I know why IE is displaying as undefined??

Thanks in advance!!!

2 Answers 2

4

That's because IE7 doesn't support accessing strings by index.

This works in all browsers:

c = (b = x[1]).charAt(0);
Sign up to request clarification or add additional context in comments.

Comments

1

That's because Internet Explorer does not support the array-like character access for Strings.

In your code, you want to access the first character of the following String: "2" which is not supported in Internet Explorer.

You can use String.charAt() to achieve the same results:

c = (b = x[1]).charAt(0);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.