Important string, number, Math, array in js

MAHMUD SUFIAN
8 min readMay 5, 2021

String :

1. String.prototype.charAt()

charAt() return new string. and this default value is the index(0). The first index position is 0, you will know string position then you will call charAt() method. and pass the index number. The first string position of 0, the second is index 1, the third string is index 2. this is charAt() method work.

EXAMPLE:

const sentence = ‘Learning with programming hero’;

console.log(`learning with programming hero, first index is: ${sentence.charAt(0)}`);
// expected output: “learning with programming hero, first index is: L”

2. String.prototype.concat()

concat() method return a new string. concatenates string argument calling to a new string. if you pass the number but concate always return string.

EXAMPLE:

const string1 = ‘mahmud’;
const string2 = ‘sufian’;
const number = 5;

console.log(string1.concat(‘ ‘, string2));
// expected output: “mahmud sufian”

console.log(string1.concat(‘, ‘, string2));
// expected output: “mahmud, sufian”

console.log(string1.concat(‘ ‘, number));
// expected output: “mahmud 5”

3. String.prototype.includes()

includes() method is case-sensitive search method. this is a search for another string. and this returns true or false as appropriate.

EXAMPLE:

const sentence = ‘learning with programming hero’;

const word = ‘hero’;

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

4. String.prototype.endsWith()

endsWith() method is case sensitive. this is determined within the string last specific word or symbol and total index.

EXAMPLE:

const string1 = ‘Learning with programming hero’;

console.log(string1.endsWith(‘hero’, 30));
// expected output: true

const string2 = ‘am i learning endsWith’;

console.log(string2.endsWith(‘?’));
// expected output: false

5. String.prototype.indexOf()

indexOf() method is a case sensitive method. if you will know any string total index then you will be call indexOf() and pass the value. the value will be a string. otherwise, return -1.

EXAMPLE:

const str1 = ‘learning with programming hero’;
const number1 = str1.indexOf(‘ ‘)-1;
console.log(number1);
// expected output: 7

const str2 = ‘learning with programming hero’;
const number2 = str2.indexOf(‘hero’);
console.log(number2);
// expected output: 26

const str3 = ‘learning with programming hero’;
const number3 = str3.indexOf(‘Hero’);
console.log(number3);
// expected output: -1

6. String.prototype.lastIndexOf()

lastIndexOf() is a case sensitive method. if you will know any string total index then you will be call lastIndexOf() and pass the value. the value will be a string. otherwise, return -1.

EXAMPLE:

const str1 = ‘learning with programming hero’;
console.log(str1.lastIndexOf(“”));
// expected output: 30

const str2 = ‘learning with programming hero with jhankar mahbub’;
console.log(str2.lastIndexOf(“with”));
// expected output: 31

Note: indexOf and lastIndexOf

EXAMPLE:

const str1 = ‘learning with programming hero with jhankar mahbub’;
console.log(str1.indexOf(“with”));
// expected output: 9

const str2 = ‘learning with programming hero with jhankar mahbub’;
console.log(str2.lastIndexOf(“with”));
// expected output: 31

7. String.prototype.replace()

replace() method return new string. if you will change any string then you will use replace() method. this method pass by changeable value and new value. after that, you can use it by regEx.

EXAMPLE:

const p = ‘learning with programming hero with jhankar mahbub’;

console.log(p.replace(‘learning’, ‘travelling’));
// expected output: “travelling with programming hero”

const regex = /learning/i;
console.log(p.replace(regex, ‘journey’));
// expected output: “journey with programming hero with jhankar mahbub”

8. String.prototype.slice()

slice() method work extract string. this method not modifying and the original string extracted. this method pass by the beginIndex and endIndex.

EXAMPLE:

const p = ‘learning with programming hero’;

console.log(p.slice(9));
// expected output: “with programming hero”

console.log(p.slice(9, 25));
// expected output: “with programming”

console.log(p.slice(-4));
// expected output: “hero”

console.log(p.slice(-16, -5));
// expected output: “programming”

9. String.prototype.split()

split() method is a string divided method. this method pass by value. when you want to divide.

EXMPLE:

const str = ‘learning with programming hero’;

const word = str.split(‘ ‘);
console.log(word[1]);
// expected output: “with”

const char = str.split(‘’);
console.log(char[5]);
// expected output: “i”

const strCopy = str.split();
console.log(strCopy);
// expected output: Array [“learning with programming hero”]

10. String.prototype.startsWith()

startsWith() method is determined to begin a specific string within the target string. this method passes begging character and index position. this is true or false return.

EXAMPLE:

const string = ‘learning with programming hero’;

console.log(string.startsWith(‘lea’));
// expected output: true

console.log(string.startsWith(‘lea’, 0));
// expected output: true

console.log(string.startsWith(‘lea’, 2));
// expected output: false

11. String.prototype.substr()

substr() method returns a new string. if you want to slice any string then you will use substr() method and pass startIndex and length.

example:

const str = ‘programming’;

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

console.log(str.substr(2));
// expected output: “ogramming"

12. String.prototype.toLowerCase()

toLowerCase() method calling string and convert to lower case.

EXAMPLE:

const sentence = ‘Learning With Programming Hero’;

console.log(sentence.toLowerCase());
// expected output: “learning with programming hero”

13. String.prototype.toUpperCase()

toUpperCase() method calling string and convert to upper case.

EXAMPLE:

const sentence = ‘Learning With Programming Hero’;

console.log(sentence.toUpperCase());
// expected output: “LEARNING WITH PROGRAMMING HERO”

14. String.prototype.trim()

trim() method return new string. this method work remove white space.

EXAMPLE:

const sentence = ‘ programming hero! ‘;

console.log(sentence);
// expected output: “ programming hero! “;

console.log(sentence.trim());
// expected output: “programming hero!”;

15. String.prototype.trimEnd()

trimEnd() method return new string. this method work remove trim right white space.

EXAMPLE:

const sentence = ‘ programming hero! ‘;

console.log(sentence);
// expected output: “ programming hero! “;

console.log(sentence.trimEnd());
// expected output: “ programming hero!”;

16. String.prototype.trimStart()

trimStart() method return new string. this method work to remove trim left or beginning white space.

EXAMPLE:

const sentence = ‘ programming hero! ‘;

console.log(sentence);
// expected output: “ programming hero! “

console.log(sentence.trimStart());
// expected output: “programming hero! “

Number :

17. Number.isNaN()

isNaN() method determine types of number.

EXAMPLE:

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

console.log(typeOfNaN(‘1222r’));
// expected output: “NaN”

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

18. Number.parseFloat()

parseFloat() method is parse argument. this method string converts to a number. this is a work floating number.

EXAMPLE:

function halfPi(r) {
if (Number.isNaN(Number.parseFloat(r))) {
return 0;
}
return parseFloat(r) * 2.0 * Math.PI ;
}

console.log(halfPi(‘4.567abcdefgh’));
// expected output: 28.695307297889173

console.log(halfPi(‘abcdefgh’));
// expected output: 0

19. Number.parseInt()

parseInt() method is parse argument. this method string converts to a number. this is a work integer number.

EXAMPLE:

function rough(x, base) {
const parsed = Number.parseInt(x, base);
if (Number.isNaN(parsed)) {
return 0;
}
return parsed * 100;
}

console.log(rough(‘ 0xF’, 16));
// expected output: 1500

console.log(rough(‘321’, 2));
// expected output: 0

Math :

20. Math.abs()

Math.abs() function return absolute value. this function pass a number.

EXAMPLE:

function number(a, b) {
return Math.abs(a — b);
}

console.log(number(3, 5));
// expected output: 2

console.log(number(5, 3));
// expected output: 2

console.log(number(1.23456, 7.89012));
// expected output: 6.6555599999999995

21. Math.ceil()

Math.ceil() function return always next up level round number.

EXAMPLE:

console.log(Math.ceil(.98));
// expected output: 1

console.log(Math.ceil(7));
// expected output: 7

console.log(Math.ceil(7.007));
// expected output: 8

console.log(Math.ceil(-7.007));
// expected output: -7

22. Math.floor()

Math.floor() function return always next down level round number.

EXAMPLE:

console.log(Math.floor(7.95));
// expected output: 7

console.log(Math.floor(7.05));
// expected output: 7

console.log(Math.floor(7));
// expected output: 7

console.log(Math.floor(-7.05));
// expected output: -8

23. Math.min()

Math.min() function always return lowest value.

EXAMPLE:

console.log(Math.min(7, 4, 3));
// expected output: 3

console.log(Math.min(-2, -5, -1));
// expected output: -5

const array1 = [7, 4, 7];

console.log(Math.min(…array1));
// expected output: 4

24. Math.max()

Math.max() function always return highest value.

EXAMPLE:

console.log(Math.max(1, 7, 3));
// expected output: 7

console.log(Math.max(-2, -3, -5));
// expected output: -2

const array1 = [1, 5, 2];

console.log(Math.max(…array1));
// expected output: 5

25. Math.random()

Math.random() function always return average scale value.

EXAMPLE:

function getRandom(max) {
return Math.floor(Math.random() * max);
}

console.log(getRandom(3));
// expected output: 0, 1 or 2

console.log(getRandom(1));
// expected output: 0

console.log(Math.random());
// expected output: a number from 0 to <1

26. Math.round()

Math.round() function always return nearest integer number.

EXAMPLE:

console.log(Math.round(0.8));
// expected output: 1

console.log(Math.round(5.93), Math.round(5.4), Math.round(5.07));
// expected output: 6 5 5

console.log(Math.round(-5.06), Math.round(-5.6), Math.round(-5.94));
// expected output: -5 -6 -6

Array :

27. Array.prototype.concat()

concat() method is merge two array. this is not change existing array.

EXAMPLE:

const array1 = [‘A’, ‘B’, ‘C’];
const array2 = [‘D’, ‘E’, ‘F’];
const array3 = array1.concat(array2);

console.log(array3);
// expected output: Array [“A”, “B”, “C”, “D”, “E”, “F”]

28. Array.prototype.filter()

filter() function work by array. and this is a functionally call. this function returns the new array of the provided array.

EXAMPLE:

const words = [‘learn’, ‘with’, ‘programming’, ‘hero’, ‘with’, ‘jhankar’, ‘mahbub’];

const result = words.filter(word => word.length > 6);

console.log(result);
// expected output: Array [“programming”, “jhankar”]

29. Array.prototype.find()

find() function work by the array. and this is a functionally call. this function returns the new array of the provided array first matching element.

EXAMPLE:

const array = [7, 15, 3, 160, 84];

const found = array.find(element => element > 10);

console.log(found);
// expected output: 15

30. Array.prototype.forEach()

forEach() function is a one kind of loop method. it is very easy.

EXAMPLE:

const array = [1, 2, 3];

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

// expected output: 1
// expected output: 2
// expected output: 3

31. Array.prototype.indexOf()

indexOf() function determine array property position index.

EXAMPLE:

const array = [‘learning’, ‘with’, ‘programming’, ‘hero’];

console.log(array.indexOf(‘with’));
// expected output: 1

// start from index 2
console.log(array.indexOf(‘with’, 1));
// expected output: 1

console.log(array.indexOf(‘giraffe’));
// expected output: -1

32. Array.prototype.join()

join() method returns new array. it wor by array property separator. separator passes the join method.

EXAMPLE:

const array = [‘learning’, ‘with’, ‘programming’, ‘hero’];

console.log(array.join());
// expected output: “learning,with,programming,hero”

console.log(array.join(‘’));
// expected output: “learningwithprogramminghero”

console.log(array.join(‘-’));
// expected output: “learning-with-programming-hero”

console.log(array.join(‘/’));
// expected output: “learning/with/programming/hero”

33. Array.prototype.map()

map() method is a new creates an array and every element call the provided element.

EXAMPLE:

const array = [3, 5, 8, 19];

// pass a function to map
const map = array.map(arr => arr * 2);

console.log(map);
// expected output: Array [6, 10, 16, 38]

34. Array.prototype.lastIndexOf()

lastIndexOf() method work array last element provided array.

EXAMPLE:

const array = [‘learn’, ‘with’, ‘programming’, ‘hero’, ‘with’, ‘jhankar’, ‘mahbub’];

console.log(array.lastIndexOf(‘with’));
// expected output: 4

console.log(array.lastIndexOf(‘hero’));
// expected output: 3

console.log(array.lastIndexOf(‘learn’));
// expected output: 0

35. Array.prototype.pop()

the pop() method is a remove last element in the provided array. and this method is changing the length of the array.

EXAMPLE:

const array = [‘learn’, ‘with’, ‘programming’, ‘hero’];

console.log(array.pop());
// expected output: “hero”

console.log(array);
// expected output: Array [“learn”, “with”, “programming”]

36. Array.prototype.push()

the push() method is an add element end of the array. and this method is changing the length of the array.

EXAMPLE:

const array = [‘learn’, ‘with’, ‘programming’, ‘hero’];

const push = array.push(‘with jhankar mahbub’);

console.log(array);
// expected output: Array [“learn”, “with”, “programming”, “hero”, “with jhankar mahbub”]

37. Array.prototype.shift()

shift() method is a remove element beginning of the array. and this method is changing the length of the array.

EXAMPLE:

const array = [1, 2, 3];

console.log(array.shift());
// expected output: 1

console.log(array);
// expected output: Array [2, 3]

38. Array.prototype.unshift()

unshift() method is an add element beginning of the array. and this method is changing the length of the array.

EXAMPLE:

const array = [1, 2, 3];

const unshift = array.unshift(7)

console.log(array);
// expected output: Array [7, 1, 2, 3]

39. Array.prototype.slice()

slice() method work extract array. this method not modifying and the original string extracted. this method pass by the beginIndex and endIndex.

EXAMPLE:

const array = [‘learn’, ‘with’, ‘programming’, ‘hero’, ‘with’, ‘jhankar’, ‘mahbub’];

console.log(array.slice(2));
// expected output: Array [“programming”, “hero”, “with”, “jhankar”, “mahbub”]

console.log(array.slice(2, 4));
// expected output: Array [“programming”, “hero”]

console.log(array.slice(1, 5));
// expected output: Array [“with”, “programming”, “hero”, “with”]

40. Array.prototype.reduce()

reduce() method execute reducer that you provide. this is return single value.

EXAMPLE:

const number = [1, 2, 3, 4];
const reducer = (accumulator, currentValue) => accumulator + currentValue;

// 1 + 2 + 3 + 4
console.log(number.reduce(reducer));
// expected output: 10

// 6 + 1 + 2 + 3 + 4
console.log(number.reduce(reducer, 6));
// expected output: 16

41. Array.prototype.reverse()

the reverse() method is reverse an array. array first element going to be the last element and the last element going to be the first element.

EXAMPLE:

const array = [1, 2, 3, 4];

console.log(array);
//expected output: Array [1, 2, 3, 4]

console.log(array.reverse());
//expected output: Array [4, 3, 2, 1]

--

--