简体   繁体   中英

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

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?

其中第一个将三个单独的参数提供给console.log,第二个将三个参数附加在一起,然后将其作为单个参数传递给console.log。

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.

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.

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:

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