How to concatenate two strings so that the second string must concatenate at the end of first string in JavaScript?

JavascriptObject Oriented ProgrammingFront End Technology

To concatenate we have string.concat() method. But it will not provide any specificity, which means the length of the string after concatenation, the specific values that have to be added, etc. So to reduce this problem javascript has provided string.padEnd() method. This method takes two parameters. One is the length and the other one is the second string that has to be added to the first string.

syntax

string.padEnd(length,"string");

It takes the length as a parameter so that it makes sure that after concatenation the length of the resulted string should match with provided length.

It takes a string as a second parameter to concatenate with the first string.

Example

In the following example, the method str.padEnd() has taken a length and a second-string as its parameters. It will concatenate the second string based on the length given by the user.

 Live Demo

<html>
<body>
<script>
   var str = "Hello";
   document.write(str.padEnd(23," glad to meet you"));
   document.write("</br>");
   document.write(str.padEnd(37," glad to meet you"));
</script>
</body>
</html>

Output

Hello glad to meet you
Hello glad to meet you glad to meet y


If the first string is greater than the length provided then no concatenation takes place.

In the following example, the initially given string is more than the length provided. So no changes will occur on the first string and the string itself will be displayed as the output.

Example

Live Demo

<html>
<body>
<script>
   var str = "Hello pleasure to meet you";
   document.write(str.padEnd(15," glad to meet you"));
</script>
</body>
</html>

Output

Hello pleasure to meet you
raja
Published on 16-Jul-2019 08:45:25
Advertisements