Javascript

String Methods

Abdur Rahman
2 min readNov 2, 2020
  1. concat() :

This method is to connect more than two words or more than two variables and create a new string.

For example:

const fastName = “Abdur”;

const lastName = “Rahman”;

console.log(fastName.concat(“ ”, lastName));

// expected output: “Abdur Rahman”

2. endsWith():

This method is using total content count until selected words in a sentence. It gives two results true or false.

For example:

const text = “I love Javascript.”;

console.log(text.endsWith(“Javascript”, 17));

// expected output: true

console.log(text.endsWith(“Javascript”, 15));

// expected output: false

3. includes():

This method is to find a word in the sentence.

For example:

const text = “I am a React Developer. Because I know React.”;

const word = “know”;

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

4. substr():

This method returns two results. The first result is to input the start and end position the show only this position letter. The second result is to input one position the show this position letter to end later.

For example:

const word = “know”;

console.log(word.substr(1, 2));
// expected output: “no”

console.log(word.substr(1));
// expected output: “now”

5. trim():

This method removes both whitespaces from start and the end of a word.

For example:

const word = “ Javascript ”;

console.log(word);
// expected output: “ Javascript ”

console.log(word.trim(word));
// expected output: “Javascript”

Number Methods

  1. isNaN():

This method is if theinput string the output NaN or input NaN the output NaN.

For example:

function createNaN(x) {
if (Number.isNaN(x)) {
return ‘Number NaN’;
}
if (isNaN(x)) {
return ‘NaN’;
}
}

console.log(createNaN(‘work’));
// expected output: “NaN”

console.log(createNaN(NaN));
// expected output: “Number NaN”

Math Methods

  1. abs():

This method does not return a negative value.

For example:

function difference(a, b) {
return Math.abs(a - b);
}

console.log(difference(5, 8));
// expected output: 3

console.log(difference(8, 5));
// expected output: 3

2. sqrt():

This method returns the root value.

For example:

function rootNumber(x) {
return (Math.sqrt(x));
}

console.log(rootNumber(64));
// expected output: 8

Array Methods

  1. every():

This method is tested in Array all elements. It returns a Boolean value.

For example:

const testArray= (value) => value< 50;

const nubArray = [18, 30, 49, 25, 10, 12, 45];

console.log(nubArray.every(testArray));
// expected output: true

2. forEach():

This method returns all Array elements in different lines.

For example:

const name= [“Rahim”, “Karim”, “Hasan”];

name.forEach(element => console.log(element));

// expected output: “Rahim”
// expected output: “Karim”
// expected output: “Hasan”

--

--