Loading
Lesson 07
Courses / JavaScript for Complete Beginners
String Concatenation in JavaScript

Video Transcript

Welcome to NBK Tech World. This lesson will be about string concatenation in JavaScript. Let's suppose we want to print to the console the following sentence. Hello my name is blank and I am blank years old. Let's say that the values in the blank are going to be filled with the values from two variables. One from the variable called name and then another from the variable age. Remember we did that before? We learned how to define a variable. We define our name and we define our age previously in another lesson. Now we're gonna combine that with this predefined string like we're gonna print to the console. Hello my name is blank and I am blank years old. So to do that we're gonna learn the cause of a concatenation. That is we're gonna combine strings with the value from variables. In this case we're gonna have this string here combined with the value from the variable name, combined with this string, combined with the value from the variable age, combined finally with this string. Okay so let's get started. First let's define the variables. Remember how to define variables? I'm gonna cut this. Let's first define the variable name. So we're gonna say cost name and you can say your name there. I'm just gonna say my name like so. Then I'm gonna define another variable for the age. Cost age is whatever your age is. I'm just gonna make something up. Alright the reply. Now as you can see there's my name is there and age is there. So we're all good. Now what we have to do is start. We want to print the message to the console. So what function allows you to do that? That's a live function from the console object. So console.log and then whatever you want to say here. So in this case we're gonna say that message. Hello my name is blank and I am blank years old except these values here have to come from the variables we defined previously. This is gonna come from name and this is going to come from age. So now to do that you got to do what's called a concatenation. You can't just put the variable name like here. If you put name like so it's just gonna say hello my name is name. It's not the actual variable here because this is just a string. Remember anything within the quotes is just a string. In this case it will just appear as it is. So you have to find a way to somehow tell it hey don't use the actual name text literally but use the value from the variable called name. So as we previously learned right if you just console.log name like so without the quotes that's actually gonna be the console.log variable. The value of the variable name substituted here so we actually get my name. But if you console.log name under quotes say you're gonna get the actual text name right which is just a string. This is not a variable. So what we have to do is what if you do like so you add a plus here and you say name. See what we're gonna get here. So name as it is appeared here and then followed by whatever the value of this variable is and this gets my name. So you can see name immediately followed by my name. So this is called a concatenation. So we concatenated this string with the value from this variable and it amounted to this string right here. Notice the spaces between the operator the plus operator don't really matter. It will not reflect in this guy. If you want actually want a space there you have to add it yourself to the string say to the name space. See the space right here. Like so. Okay now the space will appear. So following this logic here we can do the same for our big sentence as before. Let me do a console.log again. I'm gonna paste that thing we had before. Now we're gonna do I challenge you to do the same here except you have to break this guy apart right. So you cannot just have name here. You have to do a concatenation right here. So you have to use this string here make this guy's a isolated string and then you put a plus variable name and then plus the string and so on. Let's just start with the first part. Let me get this other part out of the way. Let's just start simple. Let's do hello my name is whatever right. So hello my name is and then there's a space here whatever. How can you add that whatever you can say plus and the variable name. So this is gonna say hello my name is space whatever the value of the variable the value whatever value the variable called name is holding. So as you can see you get hello comma my name is and my name which is the value stored in the variable name. Okay so this is called concatenation. All right so I challenge you to complete the sentence and do more concatenation to finish the sentence we had before which is hello my name is blank and I am blank years old period. You can pause the video and do it and then we'll do it together later. So how did it go? So we have hello my name is and then we're gonna add another string here and I am blank right. We have to have the space here otherwise there's not gonna be a space and then you put a plus by convention I always put spaces between the before and after the plus that's the convention okay you don't need any have to have it but it's a good convention to follow okay. Let me just leave it without space so you can see it doesn't really matter. I am blank so the blank is gonna be substituted with the variable here what's the name of the variable that holds the age called age like so no strings because no quotes I mean because there's not a string it's an actual variable that's holding some value that we want to be substituted here right and then I am blank space years old right. I forgot the period here and then you see hello my name is my name and I am blank the blank now substituted with the value from the variable age in my case 35 and then followed by the string space year space old followed by a period okay notice there didn't matter to have a space before after the plus it's just a good convention to make things look clean and organized okay. So let's review what we did so what we did is called a concatenation so if you have a string and you want to display in the string like the value of some variable in this case we have the name and the age stored in two variables then the variable name and the variable age you want to display them together inside a sentence right to do that we have to break the things into smaller pieces right we have to break this guy into this string followed by the variable here which is going to substitute its own value and it's going to be added to the ultimately to the final string here followed by another string followed by the variable age followed by the string okay so we use the plus sign for concatenation okay so that's it for this lesson and now you can practice do a console log of many kinds of messages you can define variables and use those variables to substitute their value in the final text for the log here okay thank you so much for watching if you like this video please click like and subscribe until the next time then bye
No comments yet (loading...)
No comments yet (loading...)
Did you like the lesson? 😆👍
Consider a donation to support our work: