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

--

--

--

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.