0

For example, what is the difference between these two? Don't they print out the same thing?

var myName = "Example"

console.log("My name is " + myName);

console.log("My name is", myName);
3
  • It doesn't look like it's really much of a difference github.com/DeveloperToolsWG/console-object/blob/master/… Commented Jun 26, 2015 at 20:16
  • 2
    This is more of the differences of console.log using a comma to separate them will log them as separate objects, + will concatenate/add them (and probably end up being a string). but you can use sprintf-like formatters in console.log: console.log("My name is %s", "Example"); Commented Jun 26, 2015 at 20:17
  • The difference appears when you want to include an object, or another non-string. Of course, it all depends on the browser too, since there are no standards for console Commented Jun 26, 2015 at 20:17

2 Answers 2

3

Yes, in the example you gave the two forms will produce the same output. However, this is behavior specific to console.log and you cannot in general join two strings using a comma.

Sign up to request clarification or add additional context in comments.

Comments

0

+ concatenates a string (i.e. combines them). In your first example, console.log takes one argument: "My name is " + myName as a single combined string. In your second example, console.log takes two arguments: "My name is" and myName, as separate strings.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.