Where to start the journey of JavaScript(Part 1)- Md Iqbal Hossain
--
Not only the Javascript programming but also all the programming languages basic concepts and pillars are the same. So, when you have decided that you are going with JavaScript let us skim over the things stated below.
- String
- Number
- Boolean
- Function
- Object
Let us discuss about “string ” in details today. Keep you eyes on my blog to explore the other essentials of Javascript core programming.
If I simply describe String, Its characters we use in our daily life. We will need to play with the characters in various aspect of programming. Lets play with string by the following criteria of javascript programming.
Create String
In any programming whatever you need, it should be created. Therefore, to play with string you need to create string first. String can be created as follows
const string1 = "This is string1";
const string2 = 'This is string2';
const string3 = `This is string3`;
Simply declare the type, name it assign the value inside double quote “string1” or single quote ‘string2’ or caret `string3
`.
Character Access
To access into character of a string charAt() method is used.
return 'string'.charAt(1) // returns "t" as index starts from 0.
Character concat
When we need to concat two or more strings concat() method is used.
const str1 = ‘Hello’;
const str2 = ‘World’;
console.log(str1.concat(‘ ‘, str2));
// expected output: “Hello World”
console.log(str2.concat(‘, ‘, str1));
// expected output: “World, Hello”
Character endsWith
This endsWith() method determines specific string at the end.
const str1 = ‘Cats are the best!’;
console.log(str1.endsWith(‘best’, 17));
// expected output: true
const str2 = ‘Is this a question’;
console.log(str2.endsWith(‘?’));
// expected output: false
indexOf in string
indexof() method locate the values in the string.
const str = 'Brave new world'
console.log('Index of first w from start is ' + str.indexOf('w')) // logs 8
console.log('Index of "new" from start is ' + str.indexOf('new')) // logs 6
toUpperCase in string
toUpperCase() method returns the called string value into upper case. Its converted to string if its not.
const sentence = ‘The quick brown fox jumps over the lazy dog.’;
console.log(sentence.toUpperCase());
// expected output: “THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.”
toLowerCase in string
toLowerCase() method returns the called string value into converted lower case.
const sentence = ‘The quick brown fox jumps over the lazy dog.’;
console.log(sentence.toLowerCase());
// expected output: “the quick brown fox jumps over the lazy dog.”
slice in string
This slice() method extracts a specified section and returns as different string without changing the previous one.
const str = ‘This is slice method.’;
console.log(str.slice(4));
// expected output: “slice method.”
console.log(str.slice(4, 9));
// expected output: “slice”
console.log(str.slice(-7));
// expected output: “method.”
console.log(str.slice(-7, -13));
// expected output: “slice”
split in string
split() method devices astring into parts and make an ordered list.
const str = ‘This is split method of javaScript programming.’;
const words = str.split(‘ ‘);
console.log(words[3]);
// expected output: “method”
const chars = str.split(‘’);
console.log(chars[8]);
// expected output: “s”
split in string
The valueOf() method returns the primitive value of a string object.
const stringObj = new String(‘new’);
console.log(stringObj);
// expected output: String { “new” }
console.log(stringObj.valueOf());
// expected output: “new”
There are many more methods are there over string. We will discuss at next.