3 Example(s) of JavaScript concat function


Description :

JavaScript Concat function is used to concat two arrays. See the code snippet:


JavaScript concat function Example - 1
<!DOCTYPE html>
<html>
 <head>
    <title>Welcome to LearnKode - A code learning platform</title>
</head>
  <body>
 <button onclick="joinArrays()">Concatinate the arrays</button>
<p id="sample"></p>
<script>
function joinArrays() {
    var cars = ["Alto", "Zen"];
    var buses = ["Punbus", "PePso", "Linus"];
    var result = cars.concat(buses); 
    document.getElementById("sample").innerHTML = result;
}
</script>

      </body>
</html>

Output

Description :

Example of JavaScript concat() function:


JavaScript concat function Example - 2
<!DOCTYPE html>
<html>
 <head>
    <title>Welcome to LearnKode - A code learning platform</title>
</head>
  <body>
<button onclick="joinArrays()">Concatinate arrays</button>
<p id="sample"></p>
<script>
function joinArrays() {
    var countries = ["India", "Pakistan"];
    var states = ["Punjab", "Haryana", "Himachal"];
    var cities =["Chandigarh","Delhi","Peshawar"];
    var result = countries.concat(states,cities); 
    document.getElementById("sample").innerHTML = result;
}
</script>

      </body>
</html>

Output

Description :

Concat-3


JavaScript concat function Example - 3
<!DOCTYPE html>
<html>
 <head>
    <title>Welcome to LearnKode - A code learning platform</title>
</head>
  <body>
<p>Click the button to join three strings into one new string.</p>
<button onclick="joinStrings()">Concatinate strings</button>
<p id="sample"></p>
<script>
function joinStrings() {
    var str1 = "Hello ";
    var str2 = "everyone!";
    var str3 = " Have a nice day!";
    var result = str1.concat(str2, str3);
    document.getElementById("sample").innerHTML = result;
}
</script>


      </body>
</html>

Output