I have been learning Javascript on my own, with the help of Videos, Books, Tutorials and more information that I have found on the internet; I have noticed that in several places they use + and , to concatenate elements, so I was wondering what is the correct way to concatenate in JS? What is the difference between using , (comma) and + (plus)? Is the same? On what occasions should I use one or the other?
I attach my example:
var numero = "numero";
var palabra = 55;
document.write(numero + palabra + "<br/>");
document.write(numero, palabra);
The syntax for write is as follows: document.write(exp1, exp2, exp3, ...)
Accepts one or more expressions.
You're actually just concatenating on the first option. By using "+" you are concatenating expression 1 with expression two, that is, you are concatenating the variables words with number, generating a new expression.
In the second option you are passing two expressions, which is completely valid according to the write syntax, however, you are not strictly concatenating.
You can use the first option when they are small or easy to visualize values. For cases where your variables are entire blocks of html I would use option two.
First, some clarifications must be made in order to understand it better:
Taking this into account, you can overload this function with many parameters, each parameter separated by commas, example:
On the other hand, the variables that this function receives can be of different types: integers, strings, floats and even booleans:
Now let's talk about string concatenation: A string is concatenated by sign
+
(and also allows you to concatenate different types of variables):If you notice, in the last example all that concatenation counts as a single variable, when we put it in a function, we call it a parameter.
So, the
write()
purpose of the function is to receive parameters and all those parameters are printed concatenatedIn the first it only receives one parameter but this parameter is already concatenated, in the second it receives 2 but here the function is responsible for concatenating them.
This is likely to be confusing with the function write being concatenated, but if we take another function as an example this would be even clearer.
The best way to concatenate in JS is the (+) because the (,) is used in many cases as parameter inclusion in functions or in other js implementations, this means that when specifying the comma (,) certain elements are valued separately "this is in certain cases" while if the (+) is included it is set as a single string or concatenation returning 2 elements as a single value,
if you are looking to concatenate I recommend using:
Using one
,
inside anconsole.write
orconsole.log
does not mean that you concatenate, but rather that you are printing two different variables in a single output.This is not concatenation, it is just an output of 3 values without writing 3 times
document.write
this is demonstrated like thisWe are seeing that each variable has a data type, if it were concatenated, it would be a whole string.
There are two simple ways to concatenate, with the operator
+
or with variable interpolationI want you to see how all prints behave either to the DOM or to the console
The concatenation by operator, if you notice it is adding the number with the boolean, this is interpreted by Javascript as 10 + 0 (Javascript intervenes in the conversion of the Bool to integer and false means 0 / true is 1) and then it is concatenating the string , the moment you concatenate the string, everything becomes a
String
and so the sum of the 3 variables in the seconddocument.write
return data type is a string.The third example we are using
interpolación
variables withliteral template
that supposes an improvement when rendering the information, since with a simple syntax you can convert everything to string and concatenate to your liking and form.It is a matter of ways of working, and you can use any depending on what you want to achieve
Using
","
to concatenate, a space is generated between the text and the variable, which with**"+"**
not. It is the only differential.