Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IE 11 - console.log is displaying undefined for object properties

This is a pretty short example. I copy and paste the code below into a file, save it, and open it. Works fine in Chrome, fails in ie 11.

The output on the console is nonsense! What's going on? Have a stumbled upon some strange bug?

test.b is clearly not undefined, as it is accessible by the JSON parser and by direct object evaluation.

Also, switching around the order of the variables in the log function does nothing to change test.b being undefined.

<!DOCTYPE html>
<html>
<head>
   <title>wtf</title>
   <script>
      var test = {a:1,b:{c:1}}
      console.log(test,JSON.stringify(test),test.b);
    </script>
</head>
<body>
    WTF IE
</body>
</html>

nonsense

like image 540
Noishe Avatar asked Aug 28 '14 07:08

Noishe


People also ask

Why does console log say undefined?

The evaluate step will always return something, like the sum of 2 + 2 or a modified array, but in this case there is nothing to evaluate. Therefore it returns undefined .

Why is object property undefined JavaScript?

The JavaScript warning "reference to undefined property" occurs when a script attempted to access an object property which doesn't exist.

Can I console log an object?

Answer: Use console. log() or JSON. stringify() Method You can use the console. log() method, if you simply wants to know what's inside an object for debugging purpose. This method will print the object in browser console.


1 Answers

I recently ran into this issue as well.

The issue was that the page i was working on had compatibility mode set to IE8.

<meta http-equiv="X-UA-Compatible" content="IE=8" >

I believe IE8 did not have console, so console.log would be undefined.

like image 115
Jgood Avatar answered Oct 10 '22 11:10

Jgood