Top JavaScript methods you should know for daily use

Top JavaScript methods you should know for daily use
September 23, 2021

JavaScript has tons of methods that can help you easily accomplish everyday software development tasks! Here are some of the most important, useful JavaScript methods to make your life easier while coding.

1.toString()

The JavaScript method toString() converts an array to a string of all the array values with a comma separating them.

const langArr = ["JavaScript","Python","PHP","Java"];

//converting an array to string
const langString = langArr.toString()

//output: 'JavaScript,Python,PHP,Java'

2. push()

Using push() on an array allows you to add an item only to the end of the array. Useful for when you want to append or prepend functionality onto your list, this method is particularly useful when dealing with infinite variables/environments.

push() returns the length of the array after adding the element into the array.

const langArr = ["JavaScript","Python","PHP","Java"];

//adding element into the array

langArr.push("Go Lang");

//output: 5

//array values: "['JavaScript', 'Python', 'PHP', 'Java', 'Go Lang']"

3.pop()

The array pop method simply removes and returns the last value in the array. 

const langArr = ["JavaScript","Python","PHP","Java","Go Lang"];

//removing the last element from the array - "Go Lang"
langArr.pop();

//output: 'Go Lang'

//array values: "['JavaScript', 'Python', 'PHP', 'Java']"

4. length

The length property of an array tells you the number of elements in the array. it returns the total number of items in an array without affecting the actual data stored.

const langArr = ["JavaScript","Python","PHP","Java","Go Lang"];

//getting length of an array
const arrLength = langArr.length;

//output: 5



5. concat()

Array concat is a method of creating an array of arrays by merging two or more arrays of the same type together. It takes an argument where you can state what elements of each array to use for the new one.

const langArr =  ["JavaScript","Python","PHP","Java"];

const frameworksArr = ['Django','Express','Angular','Laravel'];

//merging both arrays
langArr.concat(frameworksArr);

//output: "['JavaScript', 'Python', 'PHP', 'Java', 'Django', 'Express', 'Angular', 'Laravel']"

6.sort()

The sort() method sorts an array in place. It then returns the sorted array. The default sort order is ascending, which converts the elements into strings before comparing their sequences of UTF-16 code units values.

const langArr = ['JavaScript', 'Python', 'PHP', 'Java'];

//sorting langArr in ascending order
langArr.sort();

//output: "['Java', 'JavaScript', 'PHP', 'Python']"

//sorting numbers in descending order using compare function as parameter
const numbers = [12,34,11,77,33];

numbers.sort((no1, no2) => no2 - no1);

//output: '[77, 34, 33, 12, 11]'

7.includes()

The includes() method determines if an array includes a certain value among its entries, returning true or false as appropriate.

const langArr = ["JavaScript","Python","PHP","Java"];

//checking if langArr includes JavaScript or not
langArr.includes("JavaScript");

//output: true

Resources for You

ChatGPT Guide For Software Developers

Learn to use ChatGPT to stay ahead of competition

Front-End Developer Interview Kit

Today, Start preparing to get your dream job!

JavaScript Developer Kit

Start your JavaScript journey today!

Are you looking for Front-end Developer Job?

Get Front-end Interview Kit Now, And Be Prepared to Get Your Dream Job

Get Front-end Interview Kit

Newsletter for Developers!

Join our newsletter to get important Web Development and Technology Updates