10 Trips & Ticks in JavaScript

Ak Azad
2 min readNov 4, 2020

--

1. substr()

The JavaScript string is an object that represents a sequence of characters. The substr() method extracts parts of a string, beginning at the character at the specified position, and returns the specified number of characters.

example: substr()

var name= “Md. Azad Hosen”;
var result= name.substr(3, 9);
alert(result);

2. Array.splice()

Array.splice() method can be used to modify an existing array by adding new elements or by replacing existing elements. It takes three parameters. The first one is the starting index. The second parameter determines how many elements should be removed. It is kept 0 if we only want to insert an element at a certain position. And the final parameter is the new elements.

const name = [‘azad’, ‘monir’, ‘raihan’, ‘karim’];
name.splice(1, 0, ‘tarek’);
console.log(name);
name.splice(4, 1, ‘Halim’);
console.log(name);

3. Array.slice()

The slice method returns a new array. slice method is an element selected in an array
the slice methods are given two-parameter. such as starting and ending but can’t be changed the main array

example:
let student = [‘rahim’, ‘karim’, ‘mokles’, ‘tarikul’];
let result= student.slice(2)
console.log(result);

4. Array.Sort()

If we want to array element in ascending order then we used by sort() method. We can also sort strings in alphabetical order

example:
let student = [‘rahim’, ‘karim’, ‘mokles’, ‘tarikul’];
let result= student.sort()
console.log(result);

5. Array.reverse()

The reverse() method is the exact opposite of the sort() method. If we want to array element in descending order then we used by reverse(). This method also made changes in the original array.

example:
let student = [‘rahim’, ‘karim’, ‘mokles’, ‘tarikul’];
let result= student.reverse()
console.log(result);

6. String.indexOf()

String.indexOf(searchValue [, fromIndex]) returns either the index of first occurrence of searchValue or -1.
But it works weirdly when we pass an empty string the search value. It always returns whatever we pass as fromIndex as long as it isn’t greater than the length of the string.

Example:
‘hello world’.indexOf(‘’) // returns 0
‘hello world’.indexOf(‘’, 8) // returns 8

--

--

Ak Azad
0 Followers

I am AK Azad, I am studying computer science and engineering