JavaScript: console.log() gives different results than alert() - Stack Overflow

Is console.log() supposed to print out the value of a variable at the time it's called in your Jav

Is console.log() supposed to print out the value of a variable at the time it's called in your JavaScript? That was my assumption but when I run the code below in either Firefox (using Firebug) or Google Chrome (and use the built-in dev tools), I seem to get the "final" value of an array rather than the value of the array at that time. If I use alert() statements they print out what I would expect - the value of an array at the time the alert() statement is called.

var params = new Array();
var tmp = new Array('apple', 'banana', 'cat');

for (var i=0; i < tmp.length; i++) {
    params[tmp[i]] = [];
}

console.log(params);
/*
SHOWS IN CONSOLE:

- []
+ apple             ["jan", "feb", "mar", "apr"]
+ banana            ["jan", "feb", "mar", "apr"]
+ apple             ["jan", "feb", "mar", "apr"]
*/

alert( print_arr(params) );
/* 
ALERT BOX TEXT:

[apple]:
[banana]:
[cat]:
*/

console.log('===========================================');

var tmp2 = new Array('jan', 'feb', 'mar', 'apr');
for (var i=0; i < tmp.length; i++) {
    for (var j=0; j < tmp2.length; j++) {
        params[tmp[i]].push(tmp2[j]);
    }
}           

console.log(params);
/*
SHOWS IN CONSOLE:

- []
+ apple             ["jan", "feb", "mar", "apr"]
+ banana            ["jan", "feb", "mar", "apr"]
+ apple             ["jan", "feb", "mar", "apr"]
*/

alert( print_arr(params) );
/* 
ALERT BOX TEXT:

[apple]:jan,feb,mar,apr
[banana]:jan,feb,mar,apr
[cat]:jan,feb,mar,apr
*/

function print_arr(arr) {
    var str = '';
    for (var k in arr) {
        str += '[' + k + ']:' + arr[k].toString() + "\n";

    }

    return str;
}

Is console.log() supposed to print out the value of a variable at the time it's called in your JavaScript? That was my assumption but when I run the code below in either Firefox (using Firebug) or Google Chrome (and use the built-in dev tools), I seem to get the "final" value of an array rather than the value of the array at that time. If I use alert() statements they print out what I would expect - the value of an array at the time the alert() statement is called.

var params = new Array();
var tmp = new Array('apple', 'banana', 'cat');

for (var i=0; i < tmp.length; i++) {
    params[tmp[i]] = [];
}

console.log(params);
/*
SHOWS IN CONSOLE:

- []
+ apple             ["jan", "feb", "mar", "apr"]
+ banana            ["jan", "feb", "mar", "apr"]
+ apple             ["jan", "feb", "mar", "apr"]
*/

alert( print_arr(params) );
/* 
ALERT BOX TEXT:

[apple]:
[banana]:
[cat]:
*/

console.log('===========================================');

var tmp2 = new Array('jan', 'feb', 'mar', 'apr');
for (var i=0; i < tmp.length; i++) {
    for (var j=0; j < tmp2.length; j++) {
        params[tmp[i]].push(tmp2[j]);
    }
}           

console.log(params);
/*
SHOWS IN CONSOLE:

- []
+ apple             ["jan", "feb", "mar", "apr"]
+ banana            ["jan", "feb", "mar", "apr"]
+ apple             ["jan", "feb", "mar", "apr"]
*/

alert( print_arr(params) );
/* 
ALERT BOX TEXT:

[apple]:jan,feb,mar,apr
[banana]:jan,feb,mar,apr
[cat]:jan,feb,mar,apr
*/

function print_arr(arr) {
    var str = '';
    for (var k in arr) {
        str += '[' + k + ']:' + arr[k].toString() + "\n";

    }

    return str;
}
Share Improve this question asked Mar 20, 2013 at 15:47 BillBill 4,4238 gold badges30 silver badges32 bronze badges 6
  • 2 console logs the object, so any changes made to the object will get reflected in the logged objet – Arun P Johny Commented Mar 20, 2013 at 15:49
  • 3 console.log(JSON.stringify(yourObj)); - try that instead. – James Allardice Commented Mar 20, 2013 at 15:50
  • Why do you create an array and fill it with properties instead of values? Why not use Object instead? – newtover Commented Mar 20, 2013 at 15:53
  • A small ment on your coding style: Do not use new Array(a,b,c) but [a,b,c]. It is easier on the eyes and the identifier does not have to be looked up. – Kijewski Commented Mar 20, 2013 at 15:53
  • Is Chrome's JavaScript console lazy about evaluating arrays? – Andreas Commented Mar 20, 2013 at 15:54
 |  Show 1 more ment

1 Answer 1

Reset to default 8

As I said in the ments console.log(obj) does not log a string representation, it logs a reference to the actual javascript object in the memory. So any changes made to the object will get reflected in the logged instance.

If you want to trace the progressive changes made, then convert the object to a string and print to like console.log(JSON.stringify(params)).

Also you are not using params as an array, you are using it as an map. so change params to an object var params = {}

Change params to an object and use JSON.stringify to log it

var params = {};
var tmp = new Array('apple', 'banana', 'cat');

for (var i=0; i < tmp.length; i++) {
    params[tmp[i]] = [];
}

console.log(JSON.stringify(params));
/*
SHOWS IN CONSOLE:

- []
+ apple             ["jan", "feb", "mar", "apr"]
+ banana            ["jan", "feb", "mar", "apr"]
+ apple             ["jan", "feb", "mar", "apr"]
*/

alert( print_arr(params) );
/* 
ALERT BOX TEXT:

[apple]:
[banana]:
[cat]:
*/

console.log('===========================================');

var tmp2 = new Array('jan', 'feb', 'mar', 'apr');
for (var i=0; i < tmp.length; i++) {
    for (var j=0; j < tmp2.length; j++) {
        params[tmp[i]].push(tmp2[j]);
    }
}           

console.log(JSON.stringify(params));
/*
SHOWS IN CONSOLE:

- []
+ apple             ["jan", "feb", "mar", "apr"]
+ banana            ["jan", "feb", "mar", "apr"]
+ apple             ["jan", "feb", "mar", "apr"]
*/

alert( print_arr(params) );
/* 
ALERT BOX TEXT:

[apple]:jan,feb,mar,apr
[banana]:jan,feb,mar,apr
[cat]:jan,feb,mar,apr
*/

function print_arr(arr) {
    var str = '';
    for (var k in arr) {
        str += '[' + k + ']:' + arr[k].toString() + "\n";

    }

    return str;
}

Demo: Fiddle

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744869945a4598201.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信