I want to concat multiple string using concat() method it works fine but how can i make white space between them
<HTML>
<head>
<title></title>
</head>
<body>
<label id="label_one" style="color:blue;"></label><br/>
<label id="label_two" style="color:blue;"></label><br/>
<label id="label_three" style="color:blue;"></label><br/>
<script>
var one = "abc";
var two = "def";
var three = one.concat(two)
var four = three.length;
var variableLen = one.concat(three, four);
document.getElementById("label_one").innerHTML = one;
document.getElementById("label_two").innerHTML = two;
document.getElementById("label_three").innerHTML = variableLen;
</script>
</body>
</HTML>
and my output is
abcdef6 I want my output like this abc def 6 with space after concate and using concate() method of JavaScript not with concate operator(+)
I want to concat multiple string using concat() method it works fine but how can i make white space between them
<HTML>
<head>
<title></title>
</head>
<body>
<label id="label_one" style="color:blue;"></label><br/>
<label id="label_two" style="color:blue;"></label><br/>
<label id="label_three" style="color:blue;"></label><br/>
<script>
var one = "abc";
var two = "def";
var three = one.concat(two)
var four = three.length;
var variableLen = one.concat(three, four);
document.getElementById("label_one").innerHTML = one;
document.getElementById("label_two").innerHTML = two;
document.getElementById("label_three").innerHTML = variableLen;
</script>
</body>
</HTML>
and my output is
abcdef6 I want my output like this abc def 6 with space after concate and using concate() method of JavaScript not with concate operator(+)
Share Improve this question edited Mar 28, 2018 at 8:14 kakabali 4,0332 gold badges33 silver badges60 bronze badges asked Mar 28, 2018 at 7:18 RaheelRaheel 581 gold badge1 silver badge7 bronze badges 5 |5 Answers
Reset to default 10Try this [one,two].join(' ');
(Adapt to your needs as necessary, i.e. build an array of strings, then use join
)
One way of doing it is:
var one = "abc";
var two = "def";
var three = one.concat(" ", two);
var four = three.length;
var variableLen = one.concat(" ", two, " ", four);
However it might be better to use join
as another answer mentions. On a side note, you might notice that the concat()
function takes a variable number of arguments. You can achieve this in your own functions like this:
function func1() {
console.log(arguments); // => [1,2,3]
}
func1(1, 2, 3);
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments
Simply add the white space to the concat. I prefer to use ' ' to generate the space.
<HTML>
<head>
<title></title>
</head>
<body>
<label id="label_one" style="color:blue;"></label><br/>
<label id="label_two" style="color:blue;"></label><br/>
<label id="label_three" style="color:blue;"></label><br/>
<script>
var one = "abc";
var two = "def";
var three = one.concat(two)
var four= three.length;
var variableLen = one.concat(' ', three, four);
document.getElementById("label_one").innerHTML = one;
document.getElementById("label_two").innerHTML = two;
document.getElementById("label_three").innerHTML = variableLen;
</script>
</body>
</HTML>
You can try the following:
<HTML>
<head>
<title></title>
</head>
<body>
<label id="label_one" style="color:blue;"></label><br/>
<label id="label_two" style="color:blue;"></label><br/>
<label id="label_three" style="color:blue;"></label><br/>
<script>
var one = "abc";
var two = "def";
var whiteSpace = " ";
var three = one.concat(whiteSpace, two)
var four= three.length;
var variableLen = one.concat( three,whiteSpace ,four);
document.getElementById("label_one").innerHTML = one;
document.getElementById("label_two").innerHTML = two;
document.getElementById("label_three").innerHTML = variableLen;
</script>
</body>
</HTML>
try this
var one = "abc";
var two = "def";
var three = one.concat(" ",two)
var four= three.length;
var variableLen = one.concat(" ", three," ", four);
var three = one.concat(' ', two)
....var variableLen = one.concat( ' ', three, ' ', four);
– Jaromanda X Commented Mar 28, 2018 at 7:27concat()
takes a variable number of arguments as at least two answers show below. if you want the length ofthree
to exclude spaces from concatenated strings, then either work out the strings lengths independently and add together. Or strip the spaces from the string when finding a length:var four = three.replace(/\s/g, '').length
– Zach Smith Commented Mar 28, 2018 at 7:38