Loading
Lesson 26
Courses / JavaScript for Complete Beginners
Using join to Create a String From an Array in JavaScript

Video Transcript

Welcome to NBK Attack World. This lesson we will learn how to get an array and combine all the elements into a string. So this time we're going from array to the string type. So let's get started. As usual let's take an example right off the bat. Suppose I have say an array. I am going to use square brackets right here. An array of three elements and those elements each one of them will be a number. So let's think about phone numbers for example. So let me just create the array right here. First element is the number 1 2 3. Second element is going to be another number 4 5 6 and then the third element is going to be 7 8 9 0 or 1. Let's put 1. So here we have an array. Notice the square brackets right and we have 1 2 3 elements and they're separated by a comma right. I just add an extra space after the comma by convention style convention okay. So we have three numbers here. Let's say we want to somehow combine all these numbers into a string and between each number we want a dash okay. So how can we accomplish that? So we want to be able to come from here and generate this guy right here on the right hand side. So to do that we're going to use the method join on the array okay. So we're going to learn about join. So we're going from an array to a string with some separator in between each element and that's going to be a dash in our case. So let's get started. So given the array right here let's do let's first put the sky in a variable. I'm going to use lat in case I want to change it later on okay. Let's call it numbers. So this is an array right with three elements and each of the elements is a number. Now let's do numbers dot join. Then the join method on the array type okay. It requires the argument telling it what's the delimiter. What's the separator you want when I'm combining all these elements together. This case is going to be this dash right but we have to enclose it in quotes so it becomes a string. So let's see what we get. So we get each element value separated by a dash which is what the argument to the method join and that's a resulting string okay. It generates a new string from the numbers array. Numbers array remains unchanged and the moment you do the operation you get a string a new string. So that's join. The separator doesn't have to be a dash could be anything you want. For example I could put star five star. So you would get a five star star in between each element okay. So that's one example. Let's try another example now of a sentence. We want to build a sentence from an array of words. Let's build an array of words. Let's start with the square brackets. We're creating an array a list of items. Let's add a word to it. Let's have a word for each element of the array. Let's use the example there is a cat on the table. It's a fourth element cat with element on its element the element table. So we have an array right here. Each element is a string notice the quotes surrounding each sequence of characters. In this case we have a word as each element. So let's create a variable here. I'm going to use let just in case I need to change it later on. Let's call it words. This is a variable that holds an array. A list of items. Each item each element of this array is a string representing a word. Words is an array that has seven elements. Each element is a word. Let's make a sentence out of this array. The sentence is going to be a single string. All these guys combine and what's in between each word. It's a wide space. So how can we do that knowing that you can use the join method on an array. I want to see you try it by yourself. Pause the video. Once you're done we're going to do it together. So how did it go. So let's do words. Name of the variable that holds the array dot. The method is called join. Got our parentheses here because it's a function call. Now we want to join all the elements of this array using the following separator the wide space. I just type a wide space right here. Let's see what we get. We get the sentence there is a cat on the table. There is no period but we can add it easily. And I'll show you just now how to add a period. It's very simple. You can use the string concatenation technique. Remember that. So if this guy gives you the sentence all you need is to add a period. You can do so by concatenating with the plus and the period inside quotes. So the string. So you're taking this guy whose output is a string this whole thing and you're adding a period and you're going to get a new string with the period right at the end. Okay. So we use the string concatenation to add the period right there. So let's review what we did in this lesson. In this lesson we learn about the joint method of the array type. We learn how to get an array of elements and create a string with all the elements of the array combined separated by some sort of the limiter. We did our first example with three numbers inside a list. Then we generate a string with all those numbers separated by a dash. Could be some sort of phone number formatting. Then we did the example of the sentence. We have an array of words. Each word is a string. Then we want to generate a sentence a whole string. We took the array of words and use a joint with the wide space as the separator. Then we got the string with the sentence. We added the period just to as an extra using string concatenation. Okay. That's it for this lesson and I'll see you on the next one.
No comments yet (loading...)
No comments yet (loading...)
Did you like the lesson? 😆👍
Consider a donation to support our work: