10 JavaScript Array, String, Number Operations You were Looking for!!!
Tags: javascript, beginners, webdev, productivity
Published: 2023-05-23 09:46:12


Greetings!!! I am **Mehzad Galib**, a junior developer and JavaScript enthusiast. Today I’ll talk about some of the JavaScript 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. As a beginner, you may find tricky to perform operations in JavaScript array. But no need to worry, I am here to clear things up.

## JavaScript Array Operations

At first I’ll discuss JavaScript array manipulation tactics.  I want to add elements to a pre-defined array, or remove from the array. The pre-defined array is an array made with some of the top clubs in European football:

```
let clubs = ['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 of an Array

To add a club at the end of the previously declared 'clubs' array, we'll use **push()** method.

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

###Method 2: Adding Element to the Front of an Array

To add a club to the front of an array, we'll use **unShift()** method.

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

Enough with the addition, now let's look at the removal of a  club from the clubs array. 

###Method 3: Removing Last Element from an Array

**Pop()** method is useful for removing the last element from an array. 

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

###Method 4: Removing First Element from an Array

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

## JavaScript String Manipulations

##Operation 1: Addition of Two of More Strings into One

We can use concat() function for a successful concatenation or joining of two or more strings into one. The concat method returns a new string but doesn't change the existing strings. 
General syntax of concat function is: `string.concat(str2, str3, ... , strN)`
An example is shown below:
```
const str1 = 'JavaScript;
const str2 = 'blog'
console.log(str1.concat(' ', str2))  
// will return "JavaScript blog"
```

##Operation 2: Splitting Words from a Sentence

We can use **split()** method to split each word from a string by using a separator. The syntax for split function is `split(separator)`. Normally, a separator from a sentence is a white space (' '). An example is shown below:

```
const sentence = 'hello guys how are you?'
console.log(sentence.split(' '))   
// returns ['hello', 'guys', 'how', 'are', 'you?']
```
###Operation 3: Specific word or letter search within an String

If we want to find out the availability of a word or a letter containing in a string, we can use **include()** function to validate. 
```
const talk = 'we are in great danger';
console.log(talk.includes('in'))   
// returns true
```
### Removing or Cleaning Extra White Spaces from a String

To remove or clean extra white spaces from a string, **trim()** method is applied.
```
const words = '   hi   guys  ?'
console.log(words.trim())  // returns 'hi guys?'
```

##JavaScript Special Number Related Operations

###Limiting Decimal Values of a Fractional Number

Sometimes we get results as big decimal values, but to fix the numbers of them we can use **toFixed()** method. The syntax is `toFixed(no_of_digits)`. An example is shown below:
```
const number = 3.2659787466;
console.log(number.toFixed(2))   // returns 3.26
```

###Converting a String as Number to an Integer Number

**parseInt()** function can convert a string to an integer number.

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

Hope you've learned something from reading my article. I'll publish more contents regarding JavaScript or other languages soon. You can explore my profile for more, and it would mean a lot if you follow me on **dev.to**. 

You can connect with me in [LinkedIn](https://www.linkedin.com/in/mehzad-galib/) or [Facebook](https://www.facebook.com/mehzad.galib)