2

How to create a string by joining the elements of an array in JavaScript

 2 years ago
source link: https://www.laravelcode.com/post/how-to-create-a-string-by-joining-the-elements-of-an-array-in-javascript
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

How to create a string by joining the elements of an array in JavaScript

  1112 views

  2 years ago

Javascript

Use the JavaScript join() method

You can easily create a string by joining the elements of an array using the JavaScript join() method. The join() method also allow you to specify separator to separate the array elements. The default separator is comma (,). Let's take a look at the following example:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JavaScript Create String by Joining Array Elements</title>
</head> 
<body>
    <script>
        // Sample array
        var colors = ["red", "green", "blue"];
        
        // Joining array elements
        alert(colors.join()); // Outputs: red,green,blue
        alert(colors.join(", ")); // Outputs: red, green, blue
        alert(colors.join("-")); // Outputs: red-green-blue
        alert(colors.join(" + ")); // Outputs: red + green + blue
        alert(colors.join("")); // Outputs: redgreenblue
    </script>
</body>
</html>
Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK