简体   繁体   中英

Why does a comma work inside a console.log and not alert?

I have seen some code examples using a comma inside a console.log and I thought when combining strings and variables you need the + operator. Could you explain the difference?

console.log("my list items", myListItems[0]);
alert("my list items" + myListItems[0]);

A comma separates the arguments in a function call.

console.log is designed to accept multiple arguments.

alert is designed to accept exactly one argument.

console.log supports any number of arguments. alert only accepts 1.

There is a subtle difference is that console.log (and its siblings) will do an equivalent of Array.from(arguments).join(' ') so that there is a space between the arguments.

alert function does only support one argument (message) :

window.alert(message);

console.log function support multiple arguments :

console.log(obj1 [, obj2, ..., objN]);
console.log(msg [, subst1, ..., substN]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM