10 Trips & Ticks in JavaScript

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);

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’);
name.splice(4, 1, ‘Halim’);

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

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

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

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

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.

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

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.

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




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

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Jewelupp Instagram Paylaşımı #17929697197699853

Instagram Post

Create an RSS Feed from Nuxt Content with Full Body HTML

Using Express.js for Routing inside the GCP Cloud Functions

A simple project step through part 2

How to Hash Passwords Using Bcrypt

JavaScript: The World’s Most Misunderstood Programming Language


Powering Client Side Search with React Contexts

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ak Azad

Ak Azad

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

More from Medium

How To code Consistently

A Phase-1 student at Flatiron School 😄

How I built a MERN stack project in one week for my university assignment.

Take a Time Meowt: Reflecting on a first project.