Some JavaScript Array and String methods you should know as a JavaScript Developer

Become a master of JavaScript Array and String methods

In this article, I will discuss some important JavaScript Array and String methods that we need to know must to be a JavaScript developer.

So without further ado, let’s begin.

Important Array methods

1. push()

The push()method simply adds an element to the end of the array and changes the original array. If you want to add any element to the end of an Array, you can use the push().

const myArray = [1,2,3];
myArray.push(4);
console.log(myArray);
//output: [1,2,3,4]

2. pop()

The pop()method removes the last element from the given array. Remember that using the pop method will also change the length of the given array.

const myArray1 = [1,2,3,4];
myArray1.pop(4);
console.log(myArray1);
//output: [1,2,3]

3. unshift()

The unshift()method adds an element to the start of the array. It is like the push() but the main difference is push() method adds an item to the end of the array and unshift() method adds an element to the start of the array.

const arr = [1,2,3];
arr.unshift(0);
console.log(arr);
//output: [0,1,2,3]

4. shift()

The shift()method removes the first element from the array. It is like the pop() but the main difference is pop() method removes an item from the end of the array but shift() method removes the first item from the array.

const arr = [0,1,2,3];
arr.shift();
console.log(arr);
//output: [1,2,3]

5. find()

The find()method returns the first found element in the array, tested with the provided function. If the element is not found it returns undefined. I repeat one thing that the find() method will only return the first found element in the array based on the condition, you can find it clearly in the third example. In that example, we can see that output is 6 because the find() start searching for e>2 from the first element and it found 6, and find() returns it, as 6 is greater than 2.

const arr1 = [1,2,3,4];
const element = arr1.find(e=>e>2)
console.log(element);
//output: 3
const arr1 = [1,2,3,4];
const element = arr1.find(e=>e>4)
console.log(element);
//output: undefined
const arr3 = [6,2,7,4,5,];
const element4 = arr3.find(e=>e>2)
console.log(element4);
//output: 6

6. filter()

The filter() method creates a new array with all elements that pass the test implemented by the provided function. So basically, the filter() method finds out all the elements based on the condition and creates a new array based on the result.

const newArray = [6,2,7,4,5,];
const filteredElements = newArray.filter(e=>e>2)
console.log(filteredElements);
//output: [6,7,4,5]

Important String methods

1.Length

The length property returns the number of characters in the string including whitespace.

const string = "Hello Medium";
console.log(string.length);
//Returns 12

2. Slice

The slice() method extracts a part of the string and returns the extracted part in a new string. It takes two parameters. The first one is from where to start and the other one is where to stop slicing.

const string = "Hello Medium";
console.log(string.slice(5,12));
//Medium

3. Concat

The concat() method is used to join two or more strings.

const stringg = "Hello";
console.log(stringg.concat("Medium"));
//HelloMedium

4. Split

The split() method converts a string into an array. You have to pass a character such as a comma (,) or space to tell where to split the string. If nothing is passed, the string is split between each character.

const msg = 'Eight,Methods,Are,Done';
msg.split(',');
// returns the array ["Eight", "Methods", "Are", "Done"]

The Conclusion

So these were some Important JavaScript Array and String methods but you can find more usefull methods by searching or checking MDN Web Docs. This helped me in my learning journey. And I believe it will be of importance to you, too.

JavaScript can be daunting, especially if you are a beginner. But some simple guides can help us reduce the anxiety we get during the learning process.

--

--

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
Sudipto Kumar Mitra

I am a recent Computer Science graduate with knowledge of software development and design. Currently React JS and Flutter enthusiast.