Home  >  Article  >  Web Front-end  >  Introduction to the differences between alert() and console.log()_javascript skills

Introduction to the differences between alert() and console.log()_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:42:362360browse

To put it simply, alert is a pop-up prompt and console.log is a log in the debugging tool. The following is a detailed list of the differences between alert() and console.log(),

[1]alert()

[1.1] It has a blocking effect. If you don’t click OK, subsequent code cannot continue to execute

[1.2] alert() can only output string. If the alert output is an object, the toString() method will be automatically called

e.g. alert([1,2,3]);//'1,2,3'

[1.3] alert does not support the writing of multiple parameters, and can only output the first value

e.g. alert(1,2,3);//1

[2]console.log()

[2.1] Output on the print station

[2.2] Can print any type of data

e.g. console.log([1,2,3]);//[1,2,3]

[2.3] Supports writing of multiple parameters

e.g. console.log(1,2,3)// 1 2 3

The toString() method in the prototype chain outputs alert() and console.log() and gets different results

<script type="text/javascript">
var a = [1,2,3];
alert(a); //1,2,3
Array.prototype.toString = function(){
  return 'str';
}
alert(a); //str
</script>

and

<script type="text/javascript">
var a = [1,2,3];
console.log(a); //[1,2,3]
Array.prototype.toString = function(){
  return 'str';
}
console.log(a); //[1,2,3]
</script>

The reasons why the above code outputs different results are as follows:

console.log() can print any type of data. And alert() can only output string. If the alert output is an object, the toString() method will be automatically called. If you want the output of console.log() to be the same as alert, you need to call toString():

console.log(obj. toString() );

It has nothing to do with whether you write toString() or not. The toString() you write yourself just overrides the default toString() method of the object.

If you do not override the toString() method, alert() will also call the default one.

Still the same sentence: console.log() can print any type of data and will be called because you have overridden toString() yourself. If log() can only print strings, then there is no need for the log method of the console.

The main reason is that the data types expected by the two functions are different. The data type expected by alert() is string. This is equivalent to using the object in the string context, and the corresponding conversion will naturally be made. console.log() obviously accepts any type of data. Then he doesn't have to switch. That is to say, it does not need to be placed in the string context. Then OBJ is naturally his initial data type.

The above content lists the differences between alert() and console.log(). Friends who have different ideas are welcome to propose, share with everyone, and learn and progress together.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn