3

The following script prints undefined to the console for each character in the string, but works correctly in Chrome.

<script>
function main()
{
    var x = "hello world";
    for ( var i = 0; i < x.length; ++i ) {
        console.log( x[i] );
    }
}
main();
</script>

Do I have to do something to the array in order to get this to work properly in all browsers?

4 Answers 4

5

The [] is supported in some browsers but not all:

Array-like character access (the second way above) is not part of ECMAScript 3. It is a JavaScript and ECMAScript 5 feature.

For maximum compatibility, use String.charAt() instead:

<script>
function main()
{
    var x = "hello world";
    for ( var i = 0; i < x.length; ++i ) {
        console.log( x.charAt(i) );
    }
}
main();
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

IE8 does support [] perfectly also for string, see my answer
2

Older versions of IE don't support the array notation (string[x]) to access strings, use: charAt() instead.

<script>
    function main() {
        var x = "hello world";
        for ( var i = 0; i < x.length; ++i ) {
            console.log( x.charAt(i) );
        }
    }
    main();
</script>

Also, if you're directly executing your function, you could as well create a self-executing anonymous function (to preserve the scope / not pollute the global namespace)

<script>
    (function main() {
        var x = "hello world";
        for ( var i = 0; i < x.length; ++i ) {
            console.log( x.charAt(i) );
        }
    }());
</script>

Unless you have to run it from somewhere else also, of course.

7 Comments

IE8 does support it perfectly, see my answer
Then how am I logging in IE8? Simple: the IE developer tools are way older than IE 10. OP is talking about a "JavaScript array index undefined". That has nothing to do with the console.
console object is only IE10, I tested this, or see Microsoft reference if you don't trust: msdn.microsoft.com/en-us/library/hh772192(v=vs.85).aspx
Dude, I'm running IE8 here and logging stuff to the console as we speak. Also, the user says "prints undefined to the console" MS can tell me the console object doesn't exist 'till IE10, but they've been wrong before. I'm console.log()-ing to all my delight in IE8, and there's nothing MSDN can do about that.
|
-1

It's console the problem here. This object does not exist in IE Javascript engine.

If you do this it works in both

<script>
function main()
{
    var x = "hello world", result = "";
    for ( var i = 0; i < x.length; ++i )
       result += x[i];
    document.write(result); //it prints "hello world" on page
}
main();
</script>

EDIT:

  • console object does not exists until IE10 (as correctly noted by Cerbrus, unless you turn on the IE developer tool, in such case it exists also on IE8)
  • [] to access strings chars can be used in IE8+ (on IE7 it does not work yet)

4 Comments

Try to run this on IE versions that don't support console.log. They won't support array-like access of strings, either.
IE7 does not support my code, but I suppose user is not still using IE7. Anyway I updated the answer, still IE8, IE9 and IE10 DO ALL SUPPORT [], they do not need charAt. That's why I suppose the user issue is with console.log here.
The following script prints undefined to the console. There is a console :)
Correction to your edit: The console does exits in IE8/9 if the user has the development tools installed.
-1

If you use the following code, try to increase TimeOut value to maximum...

window.setTimeOut('Your Js function(), 150)

Now, it increase to

window.setTimeOut('Your Js function(), 2000)

Comments

Your Answer

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