Quick Learn 10 methods of Javascript

Satodhru Nondon
4 min readMay 5, 2021
  1. indexOf(): The indexOf() method is used to get the index of the first occurrence of specified characters in a string. The search will start from the beginning of the string. Its default index is 0. If the value is not found, it returns -1. The indexOf() method is case sensitive. The following example uses the indexOf().
let str = "I'm learning web development.";
let index = str.indexOf("e");
console.log(index);
//output: "I'm l'e'arning web development. from the beginnig is 5."

2. lastIndexOf(): The lastIndexOf() method is used to get the index of the last occurrence of specified characters in a string. indexOf() and lastIndexOf() have similarly in default index and case sensitivity. The following example uses the lastIndexOf() method.

let str = "I'm learning web development.";
let index = str.lastIndexOf("e");
console.log(index);
//output: "I'm learning web developm'e'nt. from the end is 25."

3. slice(): The slice() method extracts parts of a string and returns the extracted parts in a new string without modifying the original string. This method uses two parameters (from which index to start, before which index to end). If starting index is given but the ending index is not given, it takes to the length of the string. Let’s see an example of the slice() method:

//with two parameters
let str = "I'm learning web development.";
let newStr = str.slice(2, 12);
console.log(newStr);
// output: "m learning" as newStr.


//with one parameters
let str = "I'm learning web development.";
let newStr = str.slice(4);
console.log(newStr);
// output: "learning web development." as a newStr.

4. trim(): The trim() is a method that is used to removes whitespace from both the start side and end side of a string. The trim method doesn’t change the original string. I have to create an instance of the trim() method.

let str = "     I'm learning web development.    ";
let newStr = str.trim();
console.log(newStr);
// output: "I'm learning web development." as newStr.

Javascript Math Methods:

5. ceil(): The ceil() method is used to return the smallest integer value that is greater than or equal to a number. In other words, the ceil() method rounds a number upward and returns an integer value. If the passed argument is already an integer, the value will be the same as the passed argument. Let’s see some examples:

let age = Math.ceil(22.345);
console.log(age);
// output: age = 23.

let number = Math.ceil(5.595);
console.log(number);
// output: number = 6.

6. floor(): The floor() method is used to return the largest integer value that is less than or equal to a number. In other words, the floor() method rounds a number down and returns an integer value. It is the opposite method of ceil() method. Let’s see some examples:

let age = Math.floor(5.999);
console.log(age);
// output: age = 5.

let number = Math.floor(-5.595);
console.log(number);
// output: number = -6.

7. round(): The round() is a method used to return rounded to the nearest integer value of a number. If the decimal part is equal or greater than 5, it will be rounded upward or if the decimal part is less than 5, it will be rounded backward. If the given number is a negative number, it will return the opposite behavior. The following example uses the round() method:

// if decimal part is equal or greater than 5..
let age = Math.round(5.599);
console.log(age);
// output: age = 5.

let number = Math.round(-5.99);
console.log(number);
// output: number = -6.

// if decimal part is less than 5..
let positive = Math.round(2.3);
console.log(positive);
// output: positive= 2.

let negative = Math.round(-2.1);
console.log(negative);
// output: negative = -2.

JavaScript Array Methods:

8. pop(): The pop method deletes the last element of an array and returns the value of the element deleted. This method changes the original array length. Let’s see an example of the pop() method:

let students = ["John", "Mia", "Adam", "Ronny", "Julie"];
let notStudent = students.pop();
console.log(notStudent);
//output: the last element is "Julie". So, notStudent = "Julie";
console.log(students);
// output: After the last element is deleted. students = ["John", "Mia", "Adam", "Ronny"]

// Let's see more
let notGirl = students.pop();
console.log(students);
// output: After the last element is deleted. students = ["John", "Mia", "Adam"]

9. push(): The push() method inserts an element as the last element of an array and returns the value of the element inserted. It changes the original array length. This is the opposite method of the pop() method. Let’s see an example of the push() method:

let students = ["John", "Mia", "Adam", "Ronny", "Julie"];
let newStudent = students.push("rock");
console.log(students);
// output: students = ["John", "Mia", "Adam", "Ronny", "rock"]

// lets see again
let newGirls = students.push("Olivia", "Emma");
console.log(students);
// output: students = ["John", "Mia", "Adam", "Ronny", "rock", "Olivia", "Emma"]

10. shift(): The shift() method removes the first element of an array and returns the value of the element removed. This method changes the original array length. The shift() method is slightly similar to the pop() method, but the difference is shift() method deletes the first element, and the pop() method deletes the last element. Let’s see an example of the shift() method:

let students = ["John", "Mia", "Adam", "Ronny", "Julie"];
let notStudent = students.shift();
console.log(notStudent);
//output notStudent = "John"
console.log(students);
//output students = ["Mia", "Adam", "Ronny", "Julie"]

// lets see again
let notBoy = students.shift();
console.log(notBoy);
//output notBoy = "Mia"
console.log(students);
//output students = ["Adam", "Ronny", "Julie"]

Thank you.

--

--