<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mehzad_Galib</title>
    <description>The latest articles on DEV Community by Mehzad_Galib (@mehzad_galib).</description>
    <link>https://dev.to/mehzad_galib</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F626479%2F506e89f9-4360-4703-9fad-f4fae4d3bc72.png</url>
      <title>DEV Community: Mehzad_Galib</title>
      <link>https://dev.to/mehzad_galib</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mehzad_galib"/>
    <language>en</language>
    <item>
      <title>10 JavaScript Array, String, Number Operations You were Looking for!!!</title>
      <dc:creator>Mehzad_Galib</dc:creator>
      <pubDate>Tue, 23 May 2023 09:46:12 +0000</pubDate>
      <link>https://dev.to/mehzad_galib/10-javascript-array-string-number-operations-you-were-looking-for-47a7</link>
      <guid>https://dev.to/mehzad_galib/10-javascript-array-string-number-operations-you-were-looking-for-47a7</guid>
      <description>&lt;p&gt;Greetings!!! I am &lt;strong&gt;Mehzad Galib&lt;/strong&gt;, 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.&lt;/p&gt;

&lt;h2&gt;
  
  
  JavaScript Array Operations
&lt;/h2&gt;

&lt;p&gt;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:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let clubs = ['Barcelona', 'Real Madrid', 'Man City', 'Bayern', 'Juventus'];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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.&lt;/p&gt;

&lt;h3&gt;
  
  
  Method 1: Adding Element to the End of an Array
&lt;/h3&gt;

&lt;p&gt;To add a club at the end of the previously declared 'clubs' array, we'll use &lt;strong&gt;push()&lt;/strong&gt; method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let clubs = ['Barcelona', 'Real Madrid', 'Man City', 'Bayern', 'Juventus'];
clubs.push('PSG');    // ''PSG' will be added to the last of the clubs array
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Method 2: Adding Element to the Front of an Array
&lt;/h3&gt;

&lt;p&gt;To add a club to the front of an array, we'll use &lt;strong&gt;unShift()&lt;/strong&gt; method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let clubs = ['Barcelona', 'Real Madrid', 'Man City', 'Bayern', 'Juventus', 'PSG'];
clubs.unShift('Arsenal'); // adding 'Arsenal' to the front of an Array
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Enough with the addition, now let's look at the removal of a  club from the clubs array. &lt;/p&gt;

&lt;h3&gt;
  
  
  Method 3: Removing Last Element from an Array
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Pop()&lt;/strong&gt; method is useful for removing the last element from an array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let clubs= ['Arsenal' ,'Barcelona', 'Real Madrid', 'Man City', 'Bayern', 'Juventus', 'PSG'];
clubs.pop()     // no parameters needed, returns super with 'PSG' removed
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Method 4: Removing First Element from an Array
&lt;/h3&gt;

&lt;p&gt;To perform that, we’ll use &lt;strong&gt;shift()&lt;/strong&gt; method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let super = ['Arsenal' ,'Barcelona', 'Real Madrid', 'Man City', 'Bayern', 'Juventus'];
super.shift()   // removes first element 'Arsenal' from the array super
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  JavaScript String Manipulations
&lt;/h2&gt;

&lt;h2&gt;
  
  
  Operation 1: Addition of Two of More Strings into One
&lt;/h2&gt;

&lt;p&gt;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. &lt;br&gt;
General syntax of concat function is: &lt;code&gt;string.concat(str2, str3, ... , strN)&lt;/code&gt;&lt;br&gt;
An example is shown below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const str1 = 'JavaScript;
const str2 = 'blog'
console.log(str1.concat(' ', str2))  
// will return "JavaScript blog"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Operation 2: Splitting Words from a Sentence
&lt;/h2&gt;

&lt;p&gt;We can use &lt;strong&gt;split()&lt;/strong&gt; method to split each word from a string by using a separator. The syntax for split function is &lt;code&gt;split(separator)&lt;/code&gt;. Normally, a separator from a sentence is a white space (' '). An example is shown below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const sentence = 'hello guys how are you?'
console.log(sentence.split(' '))   
// returns ['hello', 'guys', 'how', 'are', 'you?']
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Operation 3: Specific word or letter search within an String
&lt;/h3&gt;

&lt;p&gt;If we want to find out the availability of a word or a letter containing in a string, we can use &lt;strong&gt;include()&lt;/strong&gt; function to validate.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const talk = 'we are in great danger';
console.log(talk.includes('in'))   
// returns true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Removing or Cleaning Extra White Spaces from a String
&lt;/h3&gt;

&lt;p&gt;To remove or clean extra white spaces from a string, &lt;strong&gt;trim()&lt;/strong&gt; method is applied.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const words = '   hi   guys  ?'
console.log(words.trim())  // returns 'hi guys?'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  JavaScript Special Number Related Operations
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Limiting Decimal Values of a Fractional Number
&lt;/h3&gt;

&lt;p&gt;Sometimes we get results as big decimal values, but to fix the numbers of them we can use &lt;strong&gt;toFixed()&lt;/strong&gt; method. The syntax is &lt;code&gt;toFixed(no_of_digits)&lt;/code&gt;. An example is shown below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const number = 3.2659787466;
console.log(number.toFixed(2))   // returns 3.26
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Converting a String as Number to an Integer Number
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;parseInt()&lt;/strong&gt; function can convert a string to an integer number.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const n1 = '45'
console.log(n1.parseInt())
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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 &lt;strong&gt;dev.to&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;You can connect with me in &lt;a href="https://www.linkedin.com/in/mehzad-galib/"&gt;LinkedIn&lt;/a&gt; or &lt;a href="https://www.facebook.com/mehzad.galib"&gt;Facebook&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>webdev</category>
      <category>productivity</category>
    </item>
  </channel>
</rss>
