Gokul
Gokul

Reputation: 831

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?

Upvotes: 0

Views: 1799

Answers (4)

some
some

Reputation: 49612

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:

Upvotes: 1

Laposhasú Acsa
Laposhasú Acsa

Reputation: 1580

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.

Upvotes: 1

Jorge
Jorge

Reputation: 221

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.

Upvotes: 1

James
James

Reputation: 91

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.

Upvotes: 2

Related Questions