10 JavaScript Operations You were Looking for!!! (Array, String, Number)

Mehzad Galib
3 min readMay 5, 2021

Greetings!!! I am Mehzad Galib, a junior front-end web developer and JavaScript enthusiast. Today I’ll talk about some of the JavaScript Array operations that is needed in critical data structure patterns and web applications. Much like any other programming language, JavaScript is a multi-paradigm, dynamic language with types and operators, standard built-in objects, and methods. Array is the fundamental data structure in JavaScript. As a beginner, you may find tricky to perform operations in JavaScript array. But no need worry, I am here to clear things up.

At first I’ll discuss JS array element manipulation tactics. It means I will create an array of some elements and in this array I want to increase or decrease elements. I want to add elements to a pre-defined array, or remove from it. The pre-defined array is an array made with, lets see, I am a soccer fan therefore teams that qualified for the European Super League(ESL);

let super = ['Barcelona', 'Real Madrid', 'Man City', 'Bayern', 'Juventus'];

We can add element at any positions we like, but we need to know which position we actually want to, either in the beginning, or in the end of an array.

Method 1: Adding element to the end:

To do this, we’ll use push() method.

let super = ['Barcelona', 'Real Madrid', 'Man City', 'Bayern', 'Juventus'];
super.push('PSG'); // ''PSG' will be added to the last of the super array

Method 2: Adding element to the first position:

We’ll use unShift() method;

let super = ['Barcelona', 'Real Madrid', 'Man City', 'Bayern', 'Juventus', 'PSG'];
super.unShift('Arsenal'); // adding 'Arsenal' to the front of an Array

Now it’s time to remove some clubs from the super league.

Method 3: Remove element from the array(from last position)

We’ll use pop() method

let super = ['Arsenal' ,'Barcelona', 'Real Madrid', 'Man City', 'Bayern', 'Juventus', 'PSG'];
super.pop() // no parameters needed, returns super with 'PSG' removed

Method 4: Remove element from the array(from the front)

To perform that, we’ll use shift() method.

let super = ['Arsenal' ,'Barcelona', 'Real Madrid', 'Man City', 'Bayern', 'Juventus'];
super.shift() // removes first element 'Arsenal' from the array super

Now, let’s talk about strings.

Method 1: Concat()

You can add two or more strings into one by using concat() method, the basic syntax is concat(str1, str2, ... , strN)

const str1 = 'medium;
const str2 = 'blog'
console.log(str1.concat(str2)) // will return mediumblog

Method 2: split()

You can split sentences into each words by using this method. The syntax is split(separator)

const sentence = 'hello guys how are you?'
console.log(sentence.split(' ')) // returns ['hello', 'guys', 'how', 'are', 'you?']

Method 3: includes()

This method performs a case-sensitive search to determine whether one string may be found within another string, returning true or false as appropriate. The syntax is includes(searchString, position)

const talk = 'we are in great danger';
console.log(talk.includes('in')) // returns true

Method 4: trim()

Trim method basically removes extra whitespaces from an array, the syntax is trim()

const words = '   hi   guys  ?'
console.log(words.trim()) // returns 'hi guys?'

Now some of the things about Numbers:-

Method 1: toFixed()

Sometimes we get results as big decimal values, but to fix the numbers of them we can use toFixed method. The syntax is toFixed(digits)

const number = 3.2659787466;
console.log(number.toFixed(2)) // returns 3.26

Method 2: parseInt()

To convert a string as number to an integer number, we can use parseInt.

const n1 = '45'
console.log(n1.parseInt())

That’s it for today, good night!!

--

--

Mehzad Galib

I am a junior MERN Stack developer, basically front-end focused development is my passion. I can write scalable, re-usable code for development purpose.