0

Is there a functional reason for coding a space separately, (" ") or is it specifically for readability? It seems like a needless extra step to me, will someone please explain the purpose of doing so?

e.g:

instead of :

var userName = prompt("What is your name?")
var greeting = function(userName) {
console.log("Great to see you," + " " + userName);
};'

why not just use :

var userName = prompt("What is your name?")
var greeting = function(userName) {
console.log("Great to see you, " + userName);
};

3 Answers3

4

Where did you see the top one? I would most certainly use the second one. There is no reason for the extra string concatenation. Sometimes you may see something like this when you're trying to coerce data types (e.g. numbers to strings), but in this case with all strings, there's no reason at all to do this.

For example with numbers:

var a = 2;
var b = 3;
var c = a + b;  // 5
var d = a + "" + b; // 23
alert(c);
alert(d);
Jeff Storey
  • 56,312
  • 72
  • 233
  • 406
1

That's a "needless extra step to me" too. And not more readable. I would use your second choice. There is no difference at the end.

RitchieD
  • 1,831
  • 22
  • 21
0

Sometimes you need to do so to achieve the goal of having the space there:

That is, if you have an integer you have to do the following:

8 + " "
ronnyfm
  • 1,973
  • 25
  • 31