15 C
Jaipur
Saturday, November 28, 2020

Joining Arrays in JavaScript – Linux Hint

Must read

Hot Cyber Monday Nintendo Switch deals, bundles and games

Early Cyber Monday Nintendo Switch deals have begun trickling in, as we reach the end of Black Friday weekend. We've already seen some fantastic...

The best pre-Cyber Monday iPad deal is still this surprise iPad Air 4 price cut

Loads of the Cyber Monday iPad deals cover Apple tablets that are several years old, but the risk of buying one of these is...

Daniel Dubois vs Joe Joyce live stream: how to watch the fight from anywhere tonight

After two previous false starts, this much-anticipated face off between two of Britain's best heavyweights finally gets to take place tonight. Don't miss a...

The Apple Watch 6 drops to lowest price ever in post-Black Friday deal

While Black Friday proper is technically over, you can still find incredible deals that are available today. Amazon has already started its Cyber Monday...

In JavaScript, as in many other scripting and programming languages, we often need to use arrays. Furthermore, it is often useful to combine the elements of an array into a single string. In PHP, for example, the implode function is used to join the elements of an array. In this context, “implode” can be viewed as a synonym for “join”. In JavaScript, however, there is no “implode” function; instead, there is a built-in “join” function that performs the same task. In this article, we are going to examine JavaScript’s join function in some detail.

Syntax

The join function concatenates the elements of an array into a single string. The syntax for the join function is as follows:

Here, separator is the string or string used to separate elements of the array; it can be any character or string, such as the space character (i.e., “ ”) or a string like “xyz”, but a comma is used as the default.

Examples

Now, let’s look at some examples.

First, we declare an array of letters.

let arr = [“a”, “b”, “c”, “d”, “f”]

We can call the join function for this array without providing a separator as follows, which will return the all characters from the array separated by commas:


Now, let’s see what will happen if we provide the space character as a separator:


Here, in the string returned, the array elements are separated by the space character instead of a comma.

We can provide any character or string as a separator. If we want to put “ and ” between the array’s elements, we can do that as follows:


Here, every alphabet is separated by the “ and ”, which might be very useful for certain applications. Any string can be provided as a separator for joining an array’s elements in the same way.

Conclusion

This article explains JavaScript’s join function and provides some useful examples. We can provide any string we want as a separator to join an array’s elements.

We hope you found this article useful and continue using linuxhint.com to learn about JavaScript.

Source link

- Advertisement -

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest article

Hot Cyber Monday Nintendo Switch deals, bundles and games

Early Cyber Monday Nintendo Switch deals have begun trickling in, as we reach the end of Black Friday weekend. We've already seen some fantastic...

The best pre-Cyber Monday iPad deal is still this surprise iPad Air 4 price cut

Loads of the Cyber Monday iPad deals cover Apple tablets that are several years old, but the risk of buying one of these is...

Daniel Dubois vs Joe Joyce live stream: how to watch the fight from anywhere tonight

After two previous false starts, this much-anticipated face off between two of Britain's best heavyweights finally gets to take place tonight. Don't miss a...

The Apple Watch 6 drops to lowest price ever in post-Black Friday deal

While Black Friday proper is technically over, you can still find incredible deals that are available today. Amazon has already started its Cyber Monday...

Jake Paul vs Nate Robinson live stream: how to watch tonight’s fight from anywhere

Social media star-turned-professional-boxer Jake Paul steps in the ring once more, this time to face another career-change fighter - read on for full details...