Muktarul Khan Akash
Muktarul khan Akash
2 min readNov 2, 2020

--

Easily hack fundamental JavaScript concepts with example

#javascript string

  1. String.charAt() => The String object’s charAt() method returns you a char from a string with her index number.

Example:

const sentence = “ The young boy jump over the child”;

const index = 6 ;

console.log(`the character at index ${index} is ${sentence. charAt(index)}`;

output => The character at index 6 is u

2. String.concat() => method concatenates the string arguments to the calling string and returns a new string.

Example:

const str1 = ‘I love’;

const str2 =’Bangladesh’;

cosole.log (str1.concat(‘ ’, str2));

output => I love Bangladesh

cosole.log (str2.concat(‘ ,’, str1));

output => Bangladesh, I love;

Syntax

str.concat(str2 [, ...strN])

3.String.endsWith() => method narrate whether a string ends with the characters of a specified string, returning true to false as appropriate.

Example:

const str1 = ‘You 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

Syntax

str.endsWith(searchString[, length])

4.indexOf()=> method returns the index within the calling String object of the first occurrence of the specified value, starting the search at fromIndex. Returns -1 if the value is not found.

Example:

const paragraph = ‘The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?’;

const searchTerm = ‘dog’;
const indexOfFirst = paragraph.indexOf(searchTerm);

console.log(`The index of the first “${searchTerm}” from the beginning is ${indexOfFirst}`);
// expected output: “The index of the first “dog” from the beginning is 40"

console.log(`The index of the 2nd “${searchTerm}” is ${paragraph.indexOf(searchTerm, (indexOfFirst + 1))}`);
// expected output: “The index of the 2nd “dog” is 52"

Syntax

str.indexOf(searchValue [, fromIndex])

5.String.includes() =>the method determines whether one string may be found within another string, returning true or false as appropriate.

Example:

const sentence = ‘The quick brown fox jumps over the lazy dog.’;

const word = ‘fox’;

console.log(`The word “${word}” ${sentence.includes(word) ? ‘is’ : ‘is not’} in the sentence`);
// expected output: “The word “fox” is in the sentence”

Syntax

str.includes(searchString[, position])

6.lastIndexOf()=> the method returns the index within the calling String an object of the last occurrence of the specified value, searching backward from fromIndex. Returns -1 if the value is not found.

Example:

const paragraph = ‘The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?’;

const searchTerm = ‘lazy’;

console.log(`The index of the first “${searchTerm}” from the end is ${paragraph.lastIndexOf(searchTerm)}`);
// expected output: “The index of the first “dog” from the end is 52"

Syntax

str.lastIndexOf(searchValue[, fromIndex])

--

--

Muktarul Khan Akash
Muktarul khan Akash
0 Followers
Editor for

Frontend web developer || Web developer || JavaScript developer