What is the difference between , and + in the console.log?

拟墨画扇 提交于 2021-02-20 04:32:55

问题


pt=new Date(2019,11,12,8,2,3)

console.log(pt.getFullYear()," ",pt.getMonth());

gives result 2019 " " 11

console.log(pt.getFullYear()+" "+pt.getMonth());

gives the result as 2019 11

What is the difference between using, and + in this example?


回答1:


console.log(pt.getFullYear()," ",pt.getMonth());

The above example passes three separate arguments to console.log. What it outputs depends on how console.log is implemented. It has changed over time and is little bit different between browsers. When invoked with arguments like in the example, it has access to the variables and can display them with some magic depending on type, for example if they are arrays or objects. In your example it is displayed as:

2019 " " 11

where the numbers are in blue text, indicating that it was a variable of type number, and the empty string is shown in red, indicating that is was a string.

Compare this to the following example, where it all is converted to a string before being passed to console.log in one argument:

console.log(pt.getFullYear()+" "+pt.getMonth());

where it is displayed as

2017 5

with black text, indicating that it was passed as a string in the first parameter.

The first parameter to console.log can be used as a format string, like printf in c and other languages. For example

console.log( "%d %d", pt.getFullYear(), pt.getMonth() );

where %d is a place holder for a number. The output is in black text and gives the exact same output as your second example.

console.log("%d %d", pt.getFullYear(),pt.getMonth(), pt.getDate());

In the example above, the year and month will be shown in black text, but the date will be in blue. This is because the format string only have two placeholders, but there are three arguments. console.log show the extra arguments, using the magic.

Documentation:

  • Standard
  • Google Chrome.
  • Mozilla Firefox
  • Microsoft Edge
  • Apple Safari
  • Opera



回答2:


The first of these gives three separate arguments to console.log, while the second appends the three together, then passes that as a single argument to console.log.




回答3:


With the (,) you're with the console.log you're requesting to show a separate group of items as string, making a kind of array. When you put the (+) symbol you are adding the strings, and in this case the " " is just adding a space between the first and the second string. It is called concatenation.




回答4:


console.log is part of the Console API and is accesible in various browsers. You can find its full documentation on MDN.

It states that console log has the following parameters:

obj1 ... objN

A list of JavaScript objects to output. The string representations of each of these objects are appended together in the order listed and output.

So, when you concatenate the parameters you pass only one object to the function and when you pass multiple parameters console.log will do the concatenation for you.



来源:https://stackoverflow.com/questions/44608964/what-is-the-difference-between-and-in-the-console-log

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!