#JS from Start- String method and concatenation
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
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.
Leave #JS from Start- String method and concatenation to:
Read more #stem posts
Best Posts From tykee
We have not curated any of tykee's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.
More Posts From tykee
- Hive is still beautiful
- The Good n Ugly of Cryptos
- Bug Hunting and Suggestion (70 Hive Bounty)
- Biggest Life Error you can make as a Crypto trader.
- Life and purpose
- My Recent Macbook pro experience.
- Bug Report (a Click on Profile picture)
- Cryptos, My environment and I.
- Some Facts about Cryptocurrency - Add Yours in the comment
- Crypto: Nigerian Govt are bunch of a mess.
- Have a consistent income before dabbling to the crypto space.
- When things fails for too long, change the pattern.
- Successful Cryptocurrency Investing: Wisdom over Wealth
- The 4-Hour Rule for Achieving World-Class Skills
- Why Fundamental Analysis Trumps Technical Analysis in Crypto Trading
- Embrace New Skills for Tomorrow's World
- Easiest way to grow.
- Hey guy
- Crypto Rollercoster (TIME)
- Hey Yoo!