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

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.

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

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.

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

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.

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

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?']

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

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:-

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

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!!

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.