#JS from Start- String method and concatenation

in GitPlait2 years ago (edited)

JS FROM START.png

In the last tutorial, I talked about Numbers and Math Objects in JavaScript. I shared examples about many Math objects and their uses. Today, I will talk about String methods and concatenation and other properties that are available with strings. So, no need to waste time, let's start by creating some variables here.

const teamName = 'Manchester';
const nickName = 'Red Devils';
const teamSlogan = 'United we stand divided we fall';

Then let's initialize a variable here.

let fil;

fil = teamName + nickName;
// This output of that would only take boths names and put them together without a space.  

Concatenation. 
fil = teamName + ' ' + nickName;

This will output the names and give it a proper space up.
Append

 fil =  'Mike ';
 fil += ' Tykee ';
Putting 'Tykee' there would overide 'Mike', but the + before the = make it append to the Mike. 

We can also do

fil = 'I am good fan of ' + teamName + ' United. Otherwise known as the ' + nickName;

This is basically how concatenation works, but in ES6 they introduce Template strings which made things do be easier. I will share examples about that in my next post about JS from start.

Length 

fil = teamName.length;

This will count the characters in the string and show it. Manchester is 10. 

Concat

fil = teamName.concat(' ', nickName);

This is just another way to concatenate. 

toUpperCase, toLowerCase

fil = teamName.toLowerCase();
fil = nickName.toUpperCase();
indexOf

fil = teamName.indexOf('h');
fil = teamName.lastIndexOf('h');

The opposite of indexOf is chartAt()

fil = teamName.charAt('4');

To get last character

fil = teamName.charAt(teamName.length -1);

To pull out substring

fil = teamName.substring(0, 6);

This would pull out the index of 0 to 6 from the string. 

Slice() Works almost the same with subtring but with more use. 

fil = teamName.slice(0, 6);
fil = teamName.slice(-4)

The minus will make it start from the back. 

Split
Screenshot 302.png

fil = teamSlogan.split(' ');

include()

fil = teamSlogan.includes('United');

This return true or false if you want to know if something is in a string. 
replace()
fil = teamSlogan.replace('United', 'Together');

Together will replace United in the string. 


console.log(fil);

That will be all for now. In next tutorial, I will talk about template literals. Thank you.

cover imagetykee03.png