javascript - jquery console.log and alert different value - Stack Overflow

I realy cant understand javascript. Maybe someone can explain me the difference:validationErrors[value.

I realy cant understand javascript. Maybe someone can explain me the difference:

validationErrors[value.element.name] = value.method;
console.log(validationErrors);
alert(validationErrors);

console.log(validationErrors) returns well formed array with values, and alert(validationErrors) returns empty array. Why?

I realy cant understand javascript. Maybe someone can explain me the difference:

validationErrors[value.element.name] = value.method;
console.log(validationErrors);
alert(validationErrors);

console.log(validationErrors) returns well formed array with values, and alert(validationErrors) returns empty array. Why?

Share Improve this question edited Jul 14, 2016 at 11:52 RIYAJ KHAN 15.3k5 gold badges33 silver badges55 bronze badges asked Jul 14, 2016 at 11:20 vyckiuzvyckiuz 15710 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 7

The console is more of a debugging environment and can understand the JS objects that are passed in the log function.

Alert on the other hand is a dialog box and will coerce its arguments into string values. Which is why the output is not as well formatted as the console.

Here is a little snippet of what is actually happening in the alert box.

var validationErrors = [ 2, 3, 4 ];
console.log(toString(validationErrors));
Output >> "[object Window]"

It's also best practice to use the console for logging purposes and not the alert box.

you can try this

alert(JSON.stringify(validationErrors));

Alert have some limit according to browser it varies but most You'd probably be best staying under 1000 characters though, as many browsers seem to begin to truncate after 999. And if you are giving object in alert it will never reflect the values so prefer console for object and other data type can be used in alert but its not a good practice. console give proper view of data like object in array can be studied easily in console.

Alert is used to print messages, which means it is used to display string values. When you pass anything other than string to alert, it calls .toString function of it and prints the output.

Now why did you get a blank string?

An array is supposed to have indexed values only. So you can do array[index] = "bla bla". But when you do array["something"] = "something else", it adds a property to that array (since arrays are also objects in Javascript).

According to MDN

The toString() method returns a string representing the specified array and its elements.

In simple, it will loop over length of array and join all elements with ,(ma)

But you do not have elements in it. You have set properties. So length is 0 and hence it returns ""

Following is a simulation

var a = [];
a["test"] = "foo";
console.log(a);
console.log(a.toString());
alert({})

Reference

  • Alert
  • Array.prototype.toString

All objects in Javascript are passed by reference. So when you passed something to console.log() and it would be changed in code further, in console you will see changed value. On other hand, alert() displays message box and stops code execution, so in message box you see values exactly as they are at alert()'s call time.

For debugging purposes, you may want to use browser's debugger - I remend Chrome Dev tools. There is free course from codeschool. to help you discover such great tool: https://www.codeschool./courses/discover-devtools

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

相关推荐

  • javascript - jquery console.log and alert different value - Stack Overflow

    I realy cant understand javascript. Maybe someone can explain me the difference:validationErrors[value.

    8天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信