filmov
tv
How to turn a JavaScript array into a string - Kevin Chisholm Video
Показать описание
In this video, I’ll show you how to turn a JavaScript array into a string.
Every JavaScript array has a join method, which allows you to convert all the elements of the array into a string. In this example I have a variable called foo which is an array, and it has six elements: the letters a, b, c, d, e, and f.
Now the default separator is a comma, so what that means is if I call the join method on this array, without passing any arguments, it produces a string that consists of every element in that array, separated by a comma.
So here you can see the result of the join method is the string, the letters a, b, c, d, e, and f, separated by a comma. So that’s the default. Now I can pass an argument as a separator, or pass an argument that allows me to specify what I want the separator to be.
So here I’m specifying an empty space. So now I should wind up with a string, the letters a, b, c, d, e, and f, but each letter is separated by a space. And that’s exactly what I get there. A more typical example might be a comma, where I pass a comma as the argument to the join method and I wind up with every element in the array, separated by a comma.
But we’re not limited to that. We can pretty much use any character we’d like. Here I’m using a hyphen, and in another example would be, I might use something like a forward slash.
So, it’s up to you what you choose to pass to the join method, but keep in mind that the bottom line is the default separator is a comma, so if you pass no arguments you wind up with a string of elements separated by a comma, and then you can also pass in an argument to specify which character you want to use as a separator, when the join method converts the elements of your array to a string.
If you found this video helpful, you can subscribe to my channel by clicking the red "Subscribe" button right under the video. You can also take a look at my blog, where there are many articles and tutorials about web development.
Thanks very much for watching.
Комментарии