A PHP Error was encountered

Severity: 8192

Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated

Filename: libraries/Filtered_db.php

Line Number: 23

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

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

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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>

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now