<?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: tutorials-kept-simple</title>
    <description>The latest articles on DEV Community by tutorials-kept-simple (@tutorialskeptsimple).</description>
    <link>https://dev.to/tutorialskeptsimple</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%2F496589%2Fca6fa184-79bf-4dc4-be67-356809619a06.png</url>
      <title>DEV Community: tutorials-kept-simple</title>
      <link>https://dev.to/tutorialskeptsimple</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tutorialskeptsimple"/>
    <language>en</language>
    <item>
      <title>'Single' vs "Double" quotes for strings in javascript</title>
      <dc:creator>tutorials-kept-simple</dc:creator>
      <pubDate>Wed, 11 Nov 2020 08:38:53 +0000</pubDate>
      <link>https://dev.to/tutorialskeptsimple/single-vs-double-quotes-for-strings-in-javascript-3kh1</link>
      <guid>https://dev.to/tutorialskeptsimple/single-vs-double-quotes-for-strings-in-javascript-3kh1</guid>
      <description>&lt;p&gt;Often while coding using javascript, you would have come across the use of 'single' or "double" quotes for strings and would have wondered, if there is any real difference between the two and if there is, is there an advantage of using one type of quote over the other? This article is going to answer just that! Read on!&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Table of Contents&lt;/strong&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Difference between the two quote styles&lt;/li&gt;
&lt;li&gt;Choosing the right quoting style&lt;/li&gt;
&lt;li&gt;Single vs Double Quotes - Pros and Cons&lt;/li&gt;
&lt;li&gt;Popular Quoting Styles&lt;/li&gt;
&lt;li&gt;Parting Words&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Difference between the two quoting styles&lt;/strong&gt;&lt;span id="section1"&gt;&lt;/span&gt;
&lt;/h2&gt;

&lt;p&gt;Both single (' ') and double (" ") quotes are used to represent a string in Javascript. Choosing a quoting style is up to you and there is no special semantics for one style over the other. Nevertheless, it is important to note that there is no type for a single character in javascript, everything is always a string!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;'apple' === "apple"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Some of the other key points that both styles of quoting are as follows:&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Whichever quoting style you open a string with, close it with the same style.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;'apple' //correct
"apple" //correct
"apple' //incorrect
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The system doesn't care which one you use.&lt;/li&gt;
&lt;li&gt;On German, Hungarian, Austrian, and many other keyboards, you have to use the Shift key for both single or double-quotes.&lt;/li&gt;
&lt;li&gt;On Turkish Q keyboards, we need to press &lt;strong&gt;Shift&lt;/strong&gt; for a single quote and not for a double quote!&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Choosing the right quoting style&lt;/strong&gt;&lt;span id="section2"&gt;&lt;/span&gt;
&lt;/h2&gt;

&lt;p&gt;Wise selection of quoting can help you from escaping &lt;strong&gt;single (') or double(")&lt;/strong&gt; quotes within a string. For example, if you wish to store a HTML snippet in a variable, you can use double quotes (") for HTML attribute values and use single quotes (') for enclosing the JavaScript string:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var div = '&amp;lt;div class="panel"&amp;gt;...&amp;lt;/div&amp;gt;'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Quote within a quote&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Using quotations within a string gives rise to an error. for example,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var message='Javascript's beauty is simplicity';

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There is no way for the browser to know which one is the closing quote. The interpreter sees the second quote in 'Javascript's as the ending quote - so the rest of the line becomes invalid.&lt;/p&gt;

&lt;p&gt;We can fix this by using the fact that javascript allows both single and double quotes to define a string. So in this case you can go for double-quotes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var message="Javascript's beauty is simplicity";

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An alternate method is to escape quote arks using a forward slash "\". You use a forward slash in front of the character you intend to escape. So the same message becomes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var message='Javascript\'s beauty is simplicity';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Points to remember&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;A double-quoted string can have single quotes without escaping them, conversely, a single-quoted string can have double quotes within it without having to escape them.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Double quotes ( \" ) must escape a double quote and vice versa single quotes ( \' ) must escape a single quote.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Single vs Double Quotes - Pros and Cons&lt;/strong&gt;&lt;span id="section3"&gt;&lt;/span&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Pros&lt;/strong&gt;
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;                      Single Quotes&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;                                  Double Quotes&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt; Better readability for empty strings (' ') looks   better than ("" "")&lt;/td&gt;
&lt;td&gt; In JSON the only quoting style allowed is double quotes (" ")&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt; Easier if you wish to write html within javascript&lt;/td&gt;
&lt;td&gt; Eliminates the need to escape apostrophes when writing   sentences in english&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Cons&lt;/strong&gt;
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;    Single Quotes&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;                                  Double Quotes&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;  Not supported by JSON&lt;/td&gt;
&lt;td&gt;  Must press an extra key (Shift)  when wanting to use double quotes&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Popular Quoting Style&lt;/strong&gt;&lt;span id="section4"&gt;&lt;/span&gt;
&lt;/h2&gt;

&lt;p&gt;Combing through a few popular JavaScript projects we can see a clear preference for &lt;strong&gt;single quotes&lt;/strong&gt; over &lt;strong&gt;double-quotes&lt;/strong&gt;.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt; Project&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt; Dominant quote     style&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt; lodash&lt;/td&gt;
&lt;td&gt;  &lt;strong&gt;'&lt;/strong&gt; - 99% of quotes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt; chalk&lt;/td&gt;
&lt;td&gt;  &lt;strong&gt;'&lt;/strong&gt; - 100% of quotes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt; react&lt;/td&gt;
&lt;td&gt;  &lt;strong&gt;'&lt;/strong&gt; - 90% of quotes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt; request&lt;/td&gt;
&lt;td&gt;  &lt;strong&gt;'&lt;/strong&gt; - 97% of quotes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt; commander.js&lt;/td&gt;
&lt;td&gt;  &lt;strong&gt;'&lt;/strong&gt; - 97% of quotes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt; moment&lt;/td&gt;
&lt;td&gt;  &lt;strong&gt;'&lt;/strong&gt; - 90% of quotes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt; express&lt;/td&gt;
&lt;td&gt;  &lt;strong&gt;'&lt;/strong&gt; - 92% of quotes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt; tslib&lt;/td&gt;
&lt;td&gt;  &lt;strong&gt;"&lt;/strong&gt; - 100% of quotes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt; debug&lt;/td&gt;
&lt;td&gt;  &lt;strong&gt;'&lt;/strong&gt; - 97% of quotes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt; node-fs-extra&lt;/td&gt;
&lt;td&gt;  &lt;strong&gt;'&lt;/strong&gt; - 98% of quotes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt; axios&lt;/td&gt;
&lt;td&gt;  &lt;strong&gt;'&lt;/strong&gt; - 81% of quotes&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Data obtained from &lt;a href="https://bytearcher.com/"&gt;https://bytearcher.com/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;However, a considerable number of front-end libraries prefer double quote style which might have to do with the presence of HTML fragments.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Parting words&lt;/strong&gt;&lt;span id="section5"&gt;&lt;/span&gt;
&lt;/h2&gt;

&lt;p&gt;To sum it up, try to stick with one quoting style throughout. If you are confused about which one to pick, go with the widely-used single quotes. In ES6, you also have a third option to enclose strings - the &lt;code&gt;backtick&lt;/code&gt; string.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>quotes</category>
      <category>strings</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>How to check if a value exists in an array using JavaScript?</title>
      <dc:creator>tutorials-kept-simple</dc:creator>
      <pubDate>Wed, 11 Nov 2020 04:15:35 +0000</pubDate>
      <link>https://dev.to/tutorialskeptsimple/how-to-check-if-a-value-exists-in-an-array-using-javascript-3e9a</link>
      <guid>https://dev.to/tutorialskeptsimple/how-to-check-if-a-value-exists-in-an-array-using-javascript-3e9a</guid>
      <description>&lt;p&gt;We continue with Flexiple's tutorial series to explain the code and concept behind common use cases. In this article, we will solve for a specific case: &lt;strong&gt;To check if a value exists in an array&lt;/strong&gt;.&lt;br&gt;
We then also look at its implementation in Javascript and jQuery.&lt;/p&gt;
&lt;h3&gt;
  
  
  Where can we use this?
&lt;/h3&gt;

&lt;p&gt;You might find this useful when,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You want to execute a particular script if a certain value exists in an array&lt;/li&gt;
&lt;li&gt;You want to avoid adding duplicate values to the array&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you are new to programming or quite unfamiliar with javascript, we recommend you read through the entire article, as each section of the article would be useful.&lt;/p&gt;

&lt;p&gt;However, If you are just looking for the code, you can quickly check out the section below.&lt;/p&gt;
&lt;h1&gt;
  
  
  Table of Contents
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;Code Implementation&lt;/li&gt;
&lt;li&gt;Word of Caution&lt;/li&gt;
&lt;/ul&gt;
&lt;h1&gt;
  
  
  &lt;span id="section1"&gt;Code Implementation&lt;/span&gt;
&lt;/h1&gt;

&lt;p&gt;We are going to check for a value's existence in an array in 2 different ways using jQuery and Javascript&lt;/p&gt;
&lt;h2&gt;
  
  
  1) Using jQuery
&lt;/h2&gt;

&lt;p&gt;If you are someone strongly committed to using the jQuery library, you can use the .inArray( ) method.&lt;/p&gt;

&lt;p&gt;If the function finds the value, it returns the index position of the value and -1 if it doesn't.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;jQuery.inArray( search-value, array-or-string-in-which-to-search);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Code&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//code to check if a value exists in an array using jQuery
&amp;lt;script type='text/javascript'&amp;gt;
var fruits_arr = ['Apple','Mango','Grapes','Orange','Fig','Cherry'];
var text = "Watermelon";
// Searching in Array
console.log( 'Index : ' + jQuery.inArray('Fig', fruits_arr) );
console.log( 'Index : ' + jQuery.inArray('Peach', fruits_arr ));

// Searching in String variable
console.log( 'Index : ' + jQuery.inArray( "m", text ));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Index : 4
Index : -1
Index : 5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  2) Using Javascript
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Using Loop&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The Idea behind it: We can check for the value we need by traversing the entire array using a looping function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;script type='text/javascript'&amp;gt;

//code to check if a value exists in an array using javascript for loop
var fruits_arr = ['Apple','Mango','Grapes','Orange','Fig','Cherry'];

function checkValue(value,arr){
  var status = 'Not exist';

  for(var i=0; i&amp;lt;arr.length; i++){
    var name = arr[i];
    if(name == value){
      status = 'Exist';
      break;
    }
  }

  return status;
}
console.log('status : ' + checkValue('Mango', fruits_arr) );
console.log('status : ' + checkValue('Peach', fruits_arr) );
&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;status : Exist
status : Not exist
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Code Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;From line 3 of the code, you can see that we have an array of fruits with the name fruits_arr. This contains 6 elements namely Apple, Mango, Grapes, Orange, Fig, and Cherry.&lt;/p&gt;

&lt;p&gt;The function checkValue takes 2 parameters as input, the value that needs to be searched, and the array in which the value needs to be searched.&lt;/p&gt;

&lt;p&gt;Using a for loop the function compares each element of the array with the input value you wanted to check for. If it finds a match, the function breaks and the variable status is set to Exist, else it is set to Not Exist.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Using Inbuilt function in Javascript&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;However, instead of writing a loop for this case, you can use the inbuilt function of Array.indexOf () for the same case. If the value exists, then the function will return the index value of the element, else it will return -1&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;put-array-or-string-here.indexOf()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Code&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//code to check if a value exists in an array using javascript indexOf
var fruits_arr = ['Apple','Mango','Grapes','Orange','Fig','Cherry'];

var string = "Orange";

// Find in Array
fruits_arr.indexOf('Tomato');

fruits_arr.indexOf('Grapes');

// Find in String
string.indexOf('g');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-1
2
4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you are using modern browsers you may also use the &lt;strong&gt;includes()&lt;/strong&gt; function instead of the indexOf() function&lt;br&gt;
If you are using modern browsers you may also use the includes() function instead of the &lt;strong&gt;indexOf() function&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Like  &lt;strong&gt;indexOf()&lt;/strong&gt; function, &lt;strong&gt;theincludes()&lt;/strong&gt; function also works well with &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures"&gt;primitive types.&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const symbol = Symbol('symbol');

const array = [
  'hello',
  300,
  0,
  undefined,
  null,
  symbol
];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Using includes( )&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//code to check if a value exists in an array using includes function
array.includes('hello'); // true
array.includes(300); // true
array.includes(0); // true
array.includes(undefined); // true
array.includes(null); // true
array.includes(symbol); // true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Using indexOf()&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;array.indexOf('hello') !== -1; // true
array.indexOf(300) !== -1; // true
array.indexOf(0) !== -1; // true
array.indexOf(undefined) !== -1; // true
array.indexOf(null) !== -1; // true
array.indexOf(symbol) !== -1; // true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  &lt;span id="section2"&gt; Word of Caution&lt;/span&gt;
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Case Sensitivity&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Both includes() and indexOf()function are &lt;strong&gt;case sensitive&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const array = ['MANGO'];
array.includes('mango'); // false
array.indexOf('mango') !== -1; // false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can make it case insensitive by changing the case of the array&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const array = ['MANGO'];

const sameCaseArray = array.map(value =&amp;gt; value.toLowerCase());
// ['mango']

sameCaseArray.includes('mango'); // true
sameCaseArray.indexOf('mango') !== -1; // true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For a more versatile solution, you can check out using the .some() function which works well for a diverse array of data types.&lt;/p&gt;

&lt;h2&gt;
  
  
  Caveat of IndexOf()
&lt;/h2&gt;

&lt;p&gt;One place where indexOf() and includes() differ 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 array = [NaN];

// 😄
array.includes(NaN); // true

// 😞
array.indexOf(NaN) !== -1; // false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Browser Support
&lt;/h1&gt;

&lt;p&gt;includes() function is not supported by IE and in that case you might want to use the indexOf() function to check if there is a value in a given array but keep in mind the caveat and limitations of the indexOf() function.&lt;/p&gt;

</description>
      <category>tutorial</category>
      <category>codenewbie</category>
      <category>javascript</category>
      <category>array</category>
    </item>
    <item>
      <title>How to disable or enable buttons using javascript and jQuery?</title>
      <dc:creator>tutorials-kept-simple</dc:creator>
      <pubDate>Mon, 09 Nov 2020 12:09:41 +0000</pubDate>
      <link>https://dev.to/tutorialskeptsimple/how-to-disable-or-enable-buttons-using-javascript-and-jquery-4h5m</link>
      <guid>https://dev.to/tutorialskeptsimple/how-to-disable-or-enable-buttons-using-javascript-and-jquery-4h5m</guid>
      <description>&lt;p&gt;Let us learn how to &lt;strong&gt;enable or disable buttons&lt;/strong&gt; using javascript and jQuery based on whether the input field is filled or empty.&lt;/p&gt;

&lt;p&gt;If you are a beginner or not very familiar with javascript or jQuery, we recommend that you go through the entire article. However, if you are just looking for the code, click here!&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Table of Contents&lt;/strong&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Introduction to disabling/enabling buttons&lt;/li&gt;
&lt;li&gt;Code Implementation using Javascript and jQuery&lt;/li&gt;
&lt;li&gt;Visualization&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Introduction to disabling/enabling buttons&lt;/strong&gt;&lt;span id="section1"&gt;&lt;/span&gt;
&lt;/h2&gt;

&lt;p&gt;Often while filling out web forms have you noticed how the submit button just won't work unless we have filled all the &lt;strong&gt;required&lt;/strong&gt; fields?&lt;/p&gt;

&lt;p&gt;This is done by controlling the state of the button (enabled/disabled) based on whether the input field is filled or empty. The same principle applies to checkboxes and radio buttons.&lt;/p&gt;

&lt;p&gt;Do you wish to implement such a feature on your web form too? Read on!&lt;/p&gt;

&lt;p&gt;Before diving into the code let us first look at the logic behind toggling between different states of the button.&lt;/p&gt;

&lt;h4&gt;
  
  
  Logic behind toggling between disabled and enabled states of buttons
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Set button to disabled state in the beginning&lt;/li&gt;
&lt;li&gt;If the input value of the required field is empty, let the button remain disabled. (Disabled state = TRUE)&lt;/li&gt;
&lt;li&gt;If the input value of the required field is not empty, change the state of the button to enabled. (Or set disabled state  = FALSE).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Below, we are going to see how to disable/enable a button with one required text field implemented using Javascript and jQuery.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Code Implementation for changing the state of the button&lt;/strong&gt;&lt;span id="section2"&gt; &lt;/span&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;1. Using Javascript&lt;/strong&gt;
&lt;/h3&gt;

&lt;h4&gt;
  
  
  A) HTML
&lt;/h4&gt;

&lt;p&gt;Add the following HTML Code to your editor&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//defining button and input field
&amp;lt;input class="input" type="text" placeholder="fill me"&amp;gt;
&amp;lt;button class="button"&amp;gt;Click Me&amp;lt;/button&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Code Explanation&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Using the above code we have defined two HTML elements namely an input text field and a button.&lt;/p&gt;

&lt;h4&gt;
  
  
  B) Javascript Code
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//Program to disable or enable a button using javascript
&amp;lt;script&amp;gt;
let input = document.querySelector(".input");
let button = document.querySelector(".button");

button.disabled = true; //setting button state to disabled

input.addEventListener("change", stateHandle);

function stateHandle() {
  if (document.querySelector(".input").value === "") {
    button.disabled = true; //button remains disabled
  } else {
    button.disabled = false; //button is enabled
  }
}
&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Code Explanation&lt;/strong&gt;
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Now, using javascript we store a reference to each element, namely input, and button.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;By default a button's state is enabled in HTML so by setting disabled = true, we have disabled the button for the user.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Then we add an event handler (addEventListener) to the input field with the event property change which monitors the interaction with elements.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Here we use the change property to monitor when the user types text inside the input field and run a function accordingly.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The function we run here is called the stateHandle() that gets activated every time there is a change in the status of the input field.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The function compares the value of the input field (the text field) with an empty string.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If the user has not typed anything, then the text field will be equal ( === ) to the empty string and the button will remain disabled (disabled = true).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If the user inputs text in the input field, then the button will get enabled (disabled = false).&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Complete Code&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;html&amp;gt;
&amp;lt;body&amp;gt;
&amp;lt;input class="input" type="text" placeholder="fill me"&amp;gt;
&amp;lt;button class="button"&amp;gt;Click Me&amp;lt;/button&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;script&amp;gt;
let input = document.querySelector(".input");
let button = document.querySelector(".button");
button.disabled = true;
input.addEventListener("change", stateHandle);
function stateHandle() {
  if (document.querySelector(".input").value === "") {
    button.disabled = true;
  } else {
    button.disabled = false;
  }
}
&amp;lt;/script&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Output
&lt;/h4&gt;

&lt;h5&gt;
  
  
  A) Inactive State
&lt;/h5&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fsgmo14ha0hrfic7jyrnj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fsgmo14ha0hrfic7jyrnj.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
The button is disabled as the text field is empty&lt;/p&gt;

&lt;h5&gt;
  
  
  B) Active State
&lt;/h5&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F2zrmv49j21asyao6afyz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F2zrmv49j21asyao6afyz.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
The button is enabled as the text field is not empty&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Using jQuery to enable/disable a button&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;html&amp;gt;
&amp;lt;head&amp;gt;
    &amp;lt;title&amp;gt;jQuery - Enable or Disable Button&amp;lt;/title&amp;gt;
    &amp;lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;/head&amp;gt;

&amp;lt;body&amp;gt;
      Name: &amp;lt;input type="text" id="tbName" /&amp;gt;
    &amp;lt;input type="submit" id="submit" disabled="disabled" /&amp;gt;
&amp;lt;/body&amp;gt;

&amp;lt;script&amp;gt;
    $(document).ready(function () {
        $('#tbName').on('input change', function () {
            if ($(this).val() != '') {
                $('#submit').prop('disabled', false);
            }
            else {
                $('#submit').prop('disabled', true);
            }
        });
    });
&amp;lt;/script&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;&lt;p&gt;For the jQuery method too, we start by creating an HTML button and text field (submit and tbName respectively) and setting the button to disabled state initially.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Here the ready() function is used to make the function available once the document has been loaded. 3. The .on() method in jquery attaches the event handler to the input field (tbName).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The change event will check for changes in the input field and run the function accordingly.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Just like in javascript, if the text field is empty the button remains disabled, else it gets enabled.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Here the .prop() method is used to change the state of the button.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Visualization&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;You can play around with the above code using &lt;a href="https://www.w3schools.com/js/tryit.asp?filename=tryjs_editor" rel="noopener noreferrer"&gt;this editor&lt;/a&gt; and see which part of the code does what. You can also try out different CSS options for the button etc.&lt;/p&gt;

</description>
      <category>buttons</category>
      <category>javascript</category>
      <category>tutorial</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>How to check if an array is empty using Javascript?</title>
      <dc:creator>tutorials-kept-simple</dc:creator>
      <pubDate>Sat, 07 Nov 2020 13:11:43 +0000</pubDate>
      <link>https://dev.to/tutorialskeptsimple/how-to-check-if-an-array-is-empty-using-javascript-1a4d</link>
      <guid>https://dev.to/tutorialskeptsimple/how-to-check-if-an-array-is-empty-using-javascript-1a4d</guid>
      <description>&lt;p&gt;We continue with Flexiple's tutorial series to explain the code and concept behind common use cases. In this article, we will solve for a specific case: &lt;strong&gt;To check if an array is empty using Javascript.&lt;/strong&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Where can we use this?
&lt;/h1&gt;

&lt;p&gt;You might find this useful when you want to execute a particular script if the array is empty - like &lt;a href="https://flexiple.com/disable-button-javascript/"&gt;enabling or disabling buttons&lt;/a&gt; based on if there is any input in the required field, etc.&lt;/p&gt;

&lt;p&gt;If you are new to programming or quite unfamiliar with javascript, we recommend you read through the entire article, as each section of the article would be useful.&lt;/p&gt;

&lt;p&gt;However, if you are just looking for the code, you can quickly check out the section below.&lt;/p&gt;

&lt;h1&gt;
  
  
  Table of Contents
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;Code Implementation to check if an array is empty or not using Javascript&lt;/li&gt;
&lt;li&gt;Browser Support&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  &lt;span id="section1"&gt;Code to check if an array is empty using javascript&lt;/span&gt;
&lt;/h1&gt;

&lt;p&gt;We will quickly go over the code and its demonstration to check if an array is empty or not and also see why these specific functions are used.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//To check if an array is empty using javascript
function arrayIsEmpty(array){
    //If it's not an array, return FALSE.
    if(!Array.isArray(array)){
        return FALSE;
    }
    //If it is an array, check its length property
    if(array.length == 0){
        //Return TRUE if the array is empty
        return true;
    }
    //Otherwise, return FALSE.
    return false;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Code Explanation
&lt;/h2&gt;

&lt;p&gt;Let's breakdown this code step by step!&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;First we check if a variable is an array using the Array.isArray() method.&lt;/li&gt;
&lt;li&gt;If the variable passed is an array then the condition !Array.isArray() will be False and so the variable will go to the else condition.&lt;/li&gt;
&lt;li&gt;If the variable passed is anything but an array such as, undefined or another variable type such as a string or object, the function will return False.&lt;/li&gt;
&lt;li&gt;Having confirmed that the variable is an array, now we can check the length of the array using the Array.length property.&lt;/li&gt;
&lt;li&gt;If the length of the object is 0, then the array is considered to be empty and the function will return TRUE.&lt;/li&gt;
&lt;li&gt;Else the array is not empty and the function will return False.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Demonstration of checking if the array is empty using Javascript
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var fruitArr = new Array('Apple', 'Mango', 'Grapes');

//An example of a JavaScript array that is empty.
var arrTwo = new Array();

console.log(arrayIsEmpty(fruitArr)); //returns FALSE
console.log(arrayIsEmpty(arrTwo)); //returns TRUE
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Output
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FALSE
TRUE
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Output Explanation
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;We can see here that fruitArr is an array and hence passes into the second condition to check if the length of the array is empty.&lt;/li&gt;
&lt;li&gt;Since the array has 3 elements it is not empty and therefore the function returns False.&lt;/li&gt;
&lt;li&gt;In the second case, arrTwo, it is again an array and so passes into the second condition.&lt;/li&gt;
&lt;li&gt;Here, since the array is empty, the function returns True.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Why use the Array.isArray() method?
&lt;/h2&gt;

&lt;p&gt;The Array.isArray() method is a sure shot method to check if a variable is an array or not and it automatically eliminates the cases of null and undefined without writing an additional script to check for it.&lt;/p&gt;

&lt;p&gt;The Array.isArray() method returns true for the following cases&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Array.isArray([]);
Array.isArray([3]);
Array.isArray(new Array());
Array.isArray(new Array('apple', 'mango', 'grapes'));
Array.isArray(new Array(5));
Array.isArray(Array.prototype);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: &lt;strong&gt;Array.prototype&lt;/strong&gt; itself is an array so the function Array.isArray() returns &lt;strong&gt;TRUE&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The Array.isArray() returns False for the following cases&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Array.isArray();
Array.isArray({});
Array.isArray(null);
Array.isArray(undefined);
Array.isArray(21);
Array.isArray('Random String');
Array.isArray(true);
Array.isArray(false);
Array.isArray(new Uint8Array(32));
Array.isArray({ __proto__: Array.prototype });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Can we use typeof instead of the Array.isArray?
&lt;/h2&gt;

&lt;p&gt;The answer is NO, because an array in JavaScript is an instance of the Array object and typeof would return the &lt;strong&gt;type&lt;/strong&gt; object for it.&lt;/p&gt;

&lt;p&gt;To illustrate this, consider for example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const array = ['a', 'b', 'c'];

console.log(typeof array);   // output: 'object'

console.log(array instanceof Array); // output: true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Can we use instanceof instead of Array.isArray?
&lt;/h2&gt;

&lt;p&gt;While  instanceof can be used for most cases in place Array.isArray , the Array.isArray is preferred over instanceof as it works through multiple contexts (such as frames or windows) correctly whereas instanceof does not.&lt;/p&gt;

&lt;p&gt;The reason for this is that in javascript each window or frame has its own execution environment, thus having a different scope from each other. This means that they have different built-in objects (i.e. different global objects, different constructors, etc.). This may lead to unexpected results when using instanceof, for example, for scripts passing objects from one context to another via functions.&lt;/p&gt;

&lt;p&gt;Considering such cases, it is best to simply use Array.isArray, especially when creating a framework, library, or a plugin, where the environment in which it will be used is not known in advance.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using the length property
&lt;/h2&gt;

&lt;p&gt;Once we have made sure we are dealing only with an array, we can easily check if the array is empty or not using the length property. If the length of the array is 0, then the array is empty otherwise it is not empty.&lt;/p&gt;

&lt;p&gt;One obvious question we might have is why not just use the length property at the beginning itself? Won't it make the code simpler?&lt;/p&gt;

&lt;p&gt;True, but length property can return that the variable is empty even for non-array variables so we need to ensure that we are dealing with an array first before using the length property.&lt;/p&gt;

&lt;h1&gt;
  
  
  Browser Support
&lt;/h1&gt;

&lt;p&gt;The Array.isArray method has &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray#Browser_compatibility"&gt;very good browser support&lt;/a&gt; as it is part of the ES5 specification. However, if the browsers you're targeting lack the support, you can use a polyfill for it which is given below.&lt;/p&gt;

&lt;p&gt;The polyfill works well with browsers that are compatible with ES3 specifications and works across frames.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (!Array.isArray) {
    Array.isArray = function(arg) {
        return Object.prototype.toString.call(arg) === '[object Array]';
    };
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>tutorial</category>
      <category>codenewbie</category>
      <category>javascript</category>
      <category>array</category>
    </item>
    <item>
      <title>Check if a number is Prime or not</title>
      <dc:creator>tutorials-kept-simple</dc:creator>
      <pubDate>Fri, 06 Nov 2020 11:11:25 +0000</pubDate>
      <link>https://dev.to/tutorialskeptsimple/check-if-a-number-is-prime-or-not-7h3</link>
      <guid>https://dev.to/tutorialskeptsimple/check-if-a-number-is-prime-or-not-7h3</guid>
      <description>&lt;p&gt;In this tutorial let us look at different methods to check if a number is prime or not in JavaScript and understand where exactly are these useful. &lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;prime number&lt;/strong&gt; is a natural number greater than 1 that cannot be obtained by multiplying two smaller natural numbers. All the other nonprime natural numbers greater than 1 are called &lt;strong&gt;composite numbers.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Examples of Prime numbers: 2,3,5,7,11,13 etc.&lt;br&gt;
Examples of Composite numbers: 4,6,8,9,10,12,14 etc.&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;Table of Contents&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Where are prime numbers used in real life?&lt;br&gt;
Code&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;span id="section1"&gt;  &lt;strong&gt;Where are prime numbers used in real life?&lt;/strong&gt;&lt;/span&gt;
&lt;/h2&gt;

&lt;p&gt;Prime numbers are used widely in cryptography and in-turn in encryption. Check out this article to get a clear understanding. Prime numbers are also used in computer-generated pseudo-random numbers.&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;span id="section2"&gt;&lt;strong&gt;Code&lt;/strong&gt;&lt;/span&gt;
&lt;/h2&gt;
&lt;h3&gt;
  
  
  &lt;b&gt; Version 1&lt;/b&gt;
&lt;/h3&gt;

&lt;p&gt;This version is very slow and but has the least number of lines of code. It checks if n is divisible by every integer up to the square root of the passed value. Before doing this it checks whether a value is NaN or not. NaN values are generated when arithmetic operations result in undefined or unpresentable values. The isNaN() function is used for this. It also checks if the value passed is finite or not by using the isFinite() function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//isPrime Javascript Version 1
function isPrime1(n) {
 if (isNaN(n) || !isFinite(n) || n%1 || n&amp;lt;2) return false;
 var m=Math.sqrt(n); //returns the square root of the passed value
 for (var i=2;i&amp;lt;=m;i++) if (n%i==0) return false;
 return true;
}

console.log(isPrime1(7)); //Output: True
console.log(isPrime1(6)); //Output: False

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Version 2&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;This version relatively better than the first one. It first checks if the passed value is an even number or not. After this, it proceeds to check odd divisors only, from 3 up to the square root of the passed value. At most half of the numbers between 3 and the square root of the passed value are checked.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//isPrime Javascript Version 2
function isPrime2(n) {
 if (isNaN(n) || !isFinite(n) || n%1 || n&amp;lt;2) return false;
 if (n%2==0) return (n==2);
 var m=Math.sqrt(n);
 for (var i=3;i&amp;lt;=m;i+=2) {
  if (n%i==0) return false;
 }
 return true;
}

console.log(isPrime2(7)); //Output: True
console.log(isPrime2(6)); //Output: False
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Version 3&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;This version is even better. The following checks are performed in addition to the previous versions before checking the rest of the devisors in the loop.&lt;br&gt;
Check 1: If n is divisible by 2 or 3.&lt;br&gt;
Check 2: Check only the odd divisors that are not multiples of 3.&lt;br&gt;
In this version, at least two-thirds of divisors up to the square root of n are eliminated(i.e. All the multiples of 2 &amp;amp; 3 are eliminated)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//isPrime Javascript Version 3
function isPrime3(n) {
 if (isNaN(n) || !isFinite(n) || n%1 || n&amp;lt;2) return false;
 if (n%2==0) return (n==2);
 if (n%3==0) return (n==3);
 var m=Math.sqrt(n);
 for (var i=5;i&amp;lt;=m;i+=6) {
  if (n%i==0)     return false;
  if (n%(i+2)==0) return false;
 }
 return true;
}

console.log(isPrime3(7)); //Output: True
console.log(isPrime3(6)); //Output: False
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Version 4&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;This is the fastest of all the versions. In addition to 2 and 3, multiples of 5 are also eliminated in the loop. The result of this would be that we end up checking at max quarter numbers between 2 and the square root of n.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//isPrime Javascript Version 4
isPrime = function(n) {
 if (isNaN(n) || !isFinite(n) || n%1 || n&amp;lt;2) return false;
 if (n==leastFactor(n)) return true;
 return false;
}

leastFactor = function(n){
 if (isNaN(n) || !isFinite(n)) return NaN;
 if (n==0) return 0;
 if (n%1 || n*n&amp;lt;2) return 1;
 if (n%2==0) return 2;
 if (n%3==0) return 3;
 if (n%5==0) return 5;
 var m = Math.sqrt(n);
 for (var i=7;i&amp;lt;=m;i+=30) {
  if (n%i==0)      return i;
  if (n%(i+4)==0)  return i+4;
  if (n%(i+6)==0)  return i+6;
  if (n%(i+10)==0) return i+10;
  if (n%(i+12)==0) return i+12;
  if (n%(i+16)==0) return i+16;
  if (n%(i+22)==0) return i+22;
  if (n%(i+24)==0) return i+24;
 }
 return n;
}

console.log(isPrime(7)); //Output: True
console.log(isPrime(6)); //Output: False

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>tutorial</category>
      <category>codenewbie</category>
      <category>javascript</category>
      <category>primenumbers</category>
    </item>
    <item>
      <title>findIndex method: JavaScript array</title>
      <dc:creator>tutorials-kept-simple</dc:creator>
      <pubDate>Thu, 05 Nov 2020 12:30:33 +0000</pubDate>
      <link>https://dev.to/tutorialskeptsimple/findindex-method-javascript-array-1o9c</link>
      <guid>https://dev.to/tutorialskeptsimple/findindex-method-javascript-array-1o9c</guid>
      <description>&lt;p&gt;JavaScript methods are actions that can be performed on objects. Today, let us understand how the findIndex() method works. This method was added to the array.prototype() in the JavaScript ES6. The prototype constructor allows you to add new properties and methods to the Array() object.&lt;/p&gt;

&lt;h1&gt;
  
  
  Table of Contents
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;Syntax &amp;amp; Explanation&lt;/li&gt;
&lt;li&gt;Example Code&lt;/li&gt;
&lt;li&gt;Browser Support&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  &lt;span id="section1"&gt;Syntax and explanation&lt;/span&gt;
&lt;/h1&gt;

&lt;p&gt;The findIndex() method returns the index of the first element in array that satisfies the given testing function. If no element of the array satisfies the testing condition, it returns -1.&lt;/p&gt;

&lt;p&gt;The syntax of the findIndex() method is as follows&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;findIndex(testfunc(currentValue, index, arr), thisValue)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above findIndex() method takes two arguments:&lt;br&gt;
A. testfunc&lt;br&gt;
B. thisValue&lt;/p&gt;
&lt;h2&gt;
  
  
  A. testFunc
&lt;/h2&gt;

&lt;p&gt;The testFunc() is a function that is used to execute a condition on each element of the array until the function returns true, indicating that the element satisfying the condition was found.&lt;/p&gt;

&lt;p&gt;The testFn() takes three arguments:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;currentValue: This indicates the current element in the array being processed.&lt;/li&gt;
&lt;li&gt;index: Indicates the index of the current element being processed.&lt;/li&gt;
&lt;li&gt;arr: This is the array that the findIndex() was called upon.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  B. thisValue
&lt;/h2&gt;

&lt;p&gt;It is an optional argument that is passed to the function and used as its "this" value. If it is empty, the value "undefined" will be passed as its "this" value. In JavaScript, "this" keyword refers to the object it belongs to.&lt;/p&gt;

&lt;p&gt;The findIndex() executes testFunc() for every element of the array and if true is returned by the testFunc(), findIndex() returns the index of that element and does not check for the rest of the array elements&lt;/p&gt;
&lt;h1&gt;
  
  
  &lt;span id="section2"&gt;Example Code&lt;/span&gt;
&lt;/h1&gt;

&lt;p&gt;The following example uses findIndex() method to find the first occurrence of a prime number in a given array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function isPrime(element, index, array) {
  var start = 2;
  while (start &amp;lt;= Math.sqrt(element)) {
    if (element % start &amp;lt; 1) {
      return false;
    } else {
      start++;
    }
  }
  return element &amp;gt; 1;
}

console.log([4, 6, 16, 32].findIndex(isPrime));
//Output: -1, not found
console.log([4, 7, 6, 12].findIndex(isPrime));
//Output:  1 (array[1] is 7)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  &lt;span id="section3"&gt;Browser Support&lt;/span&gt;
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;Google Chrome 45.0 and above&lt;/li&gt;
&lt;li&gt;Mozilla Firefox 25.0 and above&lt;/li&gt;
&lt;li&gt;Microsoft Edge 12.0 and above&lt;/li&gt;
&lt;li&gt;Internet Explorer does not Support findIndex() method&lt;/li&gt;
&lt;li&gt;Safari 7.1 and above&lt;/li&gt;
&lt;li&gt;Opera 32 and above&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>codenewbie</category>
      <category>tutorial</category>
      <category>javascript</category>
      <category>array</category>
    </item>
    <item>
      <title>Get the last element of an array using JavaScript</title>
      <dc:creator>tutorials-kept-simple</dc:creator>
      <pubDate>Wed, 04 Nov 2020 12:24:41 +0000</pubDate>
      <link>https://dev.to/tutorialskeptsimple/get-the-last-element-of-an-array-using-javascript-2k7n</link>
      <guid>https://dev.to/tutorialskeptsimple/get-the-last-element-of-an-array-using-javascript-2k7n</guid>
      <description>&lt;p&gt;In this tutorial, we take a look at understanding different methods of accessing the last element of an array&lt;/p&gt;

&lt;p&gt;If you are new to programming or JavaScript, we recommend you read through the entire article. However, If you are just looking for the code, you can skip to the code section below&lt;/p&gt;

&lt;p&gt;An array is a container object that holds a fixed number of values of a single type. An array’s length, once created, would remain constant/fixed. Now that we have a basic idea of what an array is, let's find the last element in an array.&lt;/p&gt;

&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
Code

&lt;ul&gt;
&lt;li&gt;Using array length property&lt;/li&gt;
&lt;li&gt;Using the slice method&lt;/li&gt;
&lt;li&gt;Using the pop method&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Performance&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;span id="section1"&gt;Code&lt;/span&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1) &lt;span id="section2"&gt;Using the array length property&lt;/span&gt;
&lt;/h3&gt;

&lt;p&gt;The length property returns the number of elements in an array. Subtracting 1 from the length of an array gives the index of the last element of an array using which the last element can be accessed. The reason we are subtracting 1 from the length is, in JavaScript, the array index numbering starts with 0. i.e. 1st element's index would 0. Therefore the last element's index would be array length-1.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let arry = [2, 4, 6, 8, 10, 12, 14, 16];
let lastElement = arry[arry.length - 1];

console.log(lastElement);
//16
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2) &lt;span id="section3"&gt;Using the slice() method&lt;/span&gt;
&lt;/h3&gt;

&lt;p&gt;The slice() method returns specific elements from an array, as a new array object. This method selects the elements starting at the given start index and ends at the given end index excluding the element at the end index. The slice() method does not modify the existing array. Providing one index value returns the element at that position &amp;amp; a negative index value calculates the index from the end of the 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 arry = [2, 4, 6, 8, 10, 12, 14, 16];
let lastElement = arry.slice(-1);

console.log(lastElement);
//16
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3) &lt;span id="section4"&gt;Using the pop() method&lt;/span&gt;
&lt;/h3&gt;

&lt;p&gt;The pop() method pops/removes the last element of an array, and returns it. This method changes the length of the 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 arry = [2, 4, 6, 8, 10, 12, 14, 16];
let lastElement = arry.pop();

console.log(lastElement);
//16
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;span id="section5"&gt;Performance&lt;/span&gt;
&lt;/h2&gt;

&lt;p&gt;Let us now use all three methods on an array to get the last element and check which method is the fastest&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let arry = [2, 4, 6, 8, 10, 12, 14, 16];
console.time('array length property');
let lastElement = arry[arry.length - 1];
console.log(lastElement);
console.timeEnd('array length property');

console.time('array slice method');
let lastElement1 = arry.slice(-1);
console.log(lastElement1);
console.timeEnd('array slice method');

console.time('array pop method');
let lastElement2 = arry.pop();
console.log(lastElement2);
console.timeEnd('array pop method');

//Output:
//16
//array length property: 13.798ms
//[ 16 ]
//array slice method: 8.839ms
//16
//array pop method: 0.138ms
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, the pop() method is fastest. You can use it if you are fine with modifying the array. If you don't want to change the array, the slice() method can be used.&lt;/p&gt;

</description>
      <category>codenewbie</category>
      <category>tutorial</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Check if a variable is of function type or not</title>
      <dc:creator>tutorials-kept-simple</dc:creator>
      <pubDate>Tue, 03 Nov 2020 10:11:26 +0000</pubDate>
      <link>https://dev.to/tutorialskeptsimple/check-if-a-variable-is-of-function-type-or-not-3cjb</link>
      <guid>https://dev.to/tutorialskeptsimple/check-if-a-variable-is-of-function-type-or-not-3cjb</guid>
      <description>&lt;p&gt;A JavaScript function is a block of code designed to perform a particular task. It is executed when it is invoked(when something calls it). A function can be either a named or an anonymous one. This article talks about how to go about checking whether a variable is of 'Function' type or not. Before we understand the different methods of implementing this and also why anyone would want to assign a function to a variable let's look at how named and anonymous functions are declared.&lt;/p&gt;

&lt;h1&gt;
  
  
  Table of Contents
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;Function declaration types&lt;/li&gt;
&lt;li&gt;Advantage of assigning a function to a variable&lt;/li&gt;
&lt;li&gt;Code&lt;/li&gt;
&lt;li&gt;Caveats&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  &lt;span id="section1"&gt;Function declaration types&lt;/span&gt;
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Named Function declaration
&lt;/h2&gt;

&lt;p&gt;This function has a named identifier associated with it which can be used to invoke the function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function functionName(parameter1, paramter2) {//code}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Anonymous Function declaration
&lt;/h2&gt;

&lt;p&gt;It is a function that is declared without any named identifier to refer to it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var anon = function(){//code }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  &lt;span id="section2"&gt;Advantage of assigning a function to a variable&lt;/span&gt;
&lt;/h1&gt;

&lt;p&gt;Assigning a function to a variable allows us to pass this variable as a parameter to another function. This is particularly useful in scenarios that require runtime flexibility. You would mainly use such functions to run a load of code in response to an event firing For example, a button being clicked using an event handler.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;myButton.onclick = function() {
 //response actions
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  &lt;span id="section3"&gt;Code&lt;/span&gt;
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Using instanceof operator
&lt;/h2&gt;

&lt;p&gt;The instanceof operator is used to check the type of objects at run time. This operator returns a Boolean value(true or false). In the example below, an IF statement is used to check if the type of parameter passed to checkFunction() is of Function type or not.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//javascript check if function-Using instanceof operator
&amp;lt;script&amp;gt;

// Declare a variable and initialize it // Declare a variable and initialize it with an anonymous function
var exampleVar = function(){/* A set of statements */};

// to check a variable is of function type or not
function checkFunction(x)
{

    if(x instanceof Function) {
        document.write("Variable is of function type");
    }
    else {
        document.write("Variable is not of function type");
    }
}

// Function call
checkFunction(exampleVar);

&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Using Strict Equality comparison (===) along with typeof operator
&lt;/h2&gt;

&lt;p&gt;In JavaScript, strict equality comparison (===) Operator is used to check whether two entities are of not only equal values but also of equal type. The typeof operator returns a string which indicates the type of the unevaluated operand. Both of these operators provide a Boolean result. This result can be compared using the IF statement to check if the object type is "Function'.&lt;br&gt;
//javascript check if function-Using Strict Equality comparison (===) along with typeof operator&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;script&amp;gt;

// Declare a variable and initialize it with an anonymous function
var exampleVar = function(){/* A set of statements */};

// to check a variable is of function type or not
function checkFunction(x)
{
    if (typeof x === "function") {
        document.write("Variable is of function type");
    }
    else {
        document.write("Variable is not of function type");
    }
}

// Function call
checkFunction(exampleVar);

&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Using object.prototype.toString
&lt;/h1&gt;

&lt;p&gt;This method uses object.prototype.toString. Every object has a toString() method, which returns ‘[object type]’ where ‘type’ is the object type. An IF statement can be used to compare if the returned value is of the type 'Function'.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//javascript check if function-Using object.prototype.toString
&amp;lt;script&amp;gt;

// Declare a variable and initialize it with an anonymous function
var exampleVar = function(){/* A set of statements */};

// to check a variable is of function type or not
function checkFunction(x)
{
    if (Object.prototype.toString.call(x) == '[object Function]')
    {
        document.write("Variable is of function type");

    }
    else {
        document.write("Variable is not of function type");
    }
}

// Function call
checkFunction(exampleVar);

&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  &lt;span id="section4"&gt;Caveats&lt;/span&gt;
&lt;/h1&gt;

&lt;p&gt;In Chrome typeof(obj) === 'function' appears to be the fastest; however, in Firefox obj instanceof Function is performs relatively better. Related Concepts&lt;/p&gt;

</description>
      <category>codenewbie</category>
      <category>tutorial</category>
      <category>javascript</category>
      <category>variable</category>
    </item>
    <item>
      <title>forEach vs map method in Javascript </title>
      <dc:creator>tutorials-kept-simple</dc:creator>
      <pubDate>Mon, 02 Nov 2020 11:06:19 +0000</pubDate>
      <link>https://dev.to/tutorialskeptsimple/foreach-vs-map-method-in-javascript-1pa7</link>
      <guid>https://dev.to/tutorialskeptsimple/foreach-vs-map-method-in-javascript-1pa7</guid>
      <description>&lt;p&gt;In this tutorial, let us look at two commonly used, seemingly similar array methods and compare them to see different they are. To begin with, let's quickly understand what a method is and then look at the syntax, functionality followed by comparing forEach and map methods. These methods help us iterate through arrays. In JavaScript, methods are actions that can be performed on objects.&lt;/p&gt;

&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Syntax &amp;amp; Explanation&lt;/li&gt;
&lt;li&gt;Example Code&lt;/li&gt;
&lt;li&gt;Ability to chain other methods&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;span id="section1"&gt;Syntax and explanation&lt;/span&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1) forEach method
&lt;/h3&gt;

&lt;p&gt;The forEach() method executes a provided function once for each element in an array. After executing the function for every array element, this method changes the values of the existing array elements according to the result of the provided function. Hence forEach() is a mutator method. Also, forEach method doesn’t return anything (undefined).&lt;/p&gt;

&lt;h3&gt;
  
  
  2) map method
&lt;/h3&gt;

&lt;p&gt;The map() method, similar to the forEach() method, executes the provided function once for each element in an array. But unlike the forEach() method, it creates a new array with the results of calling a function for every array element. Hence map() method relies on immutability. Also, map() does not execute/call the function for those array elements without values.&lt;/p&gt;

&lt;h3&gt;
  
  
  Syntax:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;array.forEach(testfunc(currentValue, index, arr), thisValue)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;array.map(testfunc(currentValue, index, arr), thisValue)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both the methods take two arguments:&lt;/p&gt;

&lt;h3&gt;
  
  
  1) testFunc
&lt;/h3&gt;

&lt;p&gt;The testFunc() is a function that is used to execute a condition on each element of the array until the function returns true, indicating that the element satisfying the condition was found.&lt;/p&gt;

&lt;p&gt;The testFn() takes three arguments:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;currentValue&lt;/strong&gt;: This indicates the current element in the array being processed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;index&lt;/strong&gt;: Indicates the index of the current element being processed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;arr&lt;/strong&gt;: This is the array that the method was called upon.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  2) thisValue
&lt;/h3&gt;

&lt;p&gt;It is an optional argument that is passed to the function and used as its "this" value. If it is empty, the value "undefined" will be passed as its "this" value. In JavaScript, "this" keyword refers to the object it belongs to.&lt;/p&gt;

&lt;p&gt;The method executes testFunc() for every element of the array and if true is returned by the testFunc().&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;span id="section2"&gt;Example Code&lt;/span&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const exampleArray = [1, 2, 3, 4, 5]
console.log(exampleArray.forEach(x =&amp;gt; x * x * x));
//Output: (undefined)
console.log(exampleArray.map(x =&amp;gt; x * x * x));
//Output: [1 , 8, 27, 64, 125 ]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;span id="section3"&gt;Ability to chain other methods&lt;/span&gt;
&lt;/h2&gt;

&lt;p&gt;Chaining methods is the ability that one can attach another method after performing one method in one continuous line of code. i.e. Repeatedly calling one method after another on an object. &lt;/p&gt;

&lt;p&gt;One of the main differences between forEach() and map() methods is their ability to chain other methods. map() is chainable but forEach isn't. This means that one could use reduce(), sort(), and other methods after map() but that's not possible with foreach() because it returns undefined.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const exampleArray = [5, 4, 3, 2, 1]
console.log(exampleArray.forEach(x =&amp;gt; x * x * x).sort(function(a, b){return a-b}););
//Output: Uncaught TypeError: Cannot read property 'reduce' of undefined
console.log(exampleArray.map(x =&amp;gt; x * x * x).sort(function(a, b){return a-b}););
//Output: [1 , 8, 27, 64, 125 ]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>codenewbie</category>
      <category>javascript</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Declaring optional function parameters in JavaScript</title>
      <dc:creator>tutorials-kept-simple</dc:creator>
      <pubDate>Fri, 30 Oct 2020 13:53:05 +0000</pubDate>
      <link>https://dev.to/tutorialskeptsimple/declaring-optional-function-parameters-in-javascript-22pn</link>
      <guid>https://dev.to/tutorialskeptsimple/declaring-optional-function-parameters-in-javascript-22pn</guid>
      <description>&lt;p&gt;We continue with Flexiple's tutorial series to explain the code and concept behind common use cases. In today's article, let's take a dive into how Optional Parameters in JavaScript can be implemented and also understand where exactly using them would prove to be most useful.&lt;/p&gt;

&lt;h1&gt;
  
  
  Table of Contents
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;What are Optional Parameters?&lt;/li&gt;
&lt;li&gt;How Optional Parameters work?&lt;/li&gt;
&lt;li&gt;Where is it most useful?&lt;/li&gt;
&lt;li&gt;Code&lt;/li&gt;
&lt;li&gt;Caveats &amp;amp; References&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  &lt;span id="section1"&gt;What are Optional Parameters?&lt;/span&gt;
&lt;/h1&gt;

&lt;p&gt;By definition, an Optional Parameter is a handy feature that enables programmers to pass less number of parameters to a function and assign a default value.&lt;/p&gt;

&lt;p&gt;Firstly, let us first understand what the word Optional Parameter means. Parameters are the names listed in the function definition. They are used to define a function and are also called formal parameters and formal arguments. In the following example, parameter1 and parameter2 are parameters of the 'exampleFunction' function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function exampleFunction(parameter1, parameter2) {
  // code
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this context, Optional Parameters are those parameters that need not always be passed i.e. they're optional. The next obvious question would be "What value is passed when you don't pass a parameter?". So let's answer that in our next section.&lt;/p&gt;

&lt;p&gt;Before moving ahead, if you find anything difficult , do checkout other Flexiple tech blogs where we cover various basic JS concepts and breaking them down so that they can be easily consumed.&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;span id="section2"&gt;How Optional Parameters work?&lt;/span&gt;
&lt;/h1&gt;

&lt;p&gt;Usually, when you don't pass parameters, 'undefined' is passed instead. But using Optional Parameters, you can define a default value. So, whenever no value or undefined is passed, a default value is passed in its place.&lt;/p&gt;

&lt;p&gt;Before we look at different ways to implement Optional Parameters in JavaScript, let's first see where we would need them.&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;span id="section3"&gt;Where is it most useful?&lt;/span&gt;
&lt;/h1&gt;

&lt;p&gt;Optional parameters are great for simplifying code, and hiding advanced but not-often-used functionality. If majority of the time you are calling a function using the same values for some parameters, you should try making those parameters optional to avoid repetition.&lt;/p&gt;

&lt;p&gt;For example, assume you are using a function to perform a google search. This function accepts the following parameters&lt;br&gt;
Parameter 1: searchEngineURL&lt;br&gt;
Parameter 2: searchString&lt;br&gt;
Now, because you always use Google search engine, you could assign a default value to the Parameter1 as Google's URL and not always pass this URL and only pass searchString each time you call the function. Hence the searchEngineURL is the optional parameter.&lt;/p&gt;
&lt;h1&gt;
  
  
  &lt;span id="section4"&gt;Code&lt;/span&gt;
&lt;/h1&gt;
&lt;h2&gt;
  
  
  Default function parameters
&lt;/h2&gt;

&lt;p&gt;In this method, you can initialize named parameters with default values whenever no value or undefined is passed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function add(a, b = 1) {
  return a + b;
}

console.log(add(1, 2));
// expected output: 3

console.log(add(1));
// expected output: 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Using undefined property
&lt;/h2&gt;

&lt;p&gt;Whenever no value or undefined is passed to a function, a conditional (IF) statement could be used to pass the default value instead if any of the parameters is undefined.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//optional parameters JavaScript-Using undefined property
function add(a, b) {
 if(a === undefined)
   {
      a = 1;
   }
 if(b === undefined)
   {
      b = 1;
   }
  return a + b;
}

console.log(add(1, 2));
// expected output: 3

console.log(add(1));
// expected output: 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Using arguments variable
&lt;/h2&gt;

&lt;p&gt;JavaScript functions have a built-in object called arguments. It contains an array of parameters. The length of this array gives the number of parameters passed. A conditional statement is used to check the number of parameters passed and pass default values in place of the undefined parameters.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//optional parameters Javascript-Using arguments variable
function add(a, b) {

    if(arguments.length == 0) // Means no parameters are passed
    {
      a = 1;
      b = 2;
    }

    if(arguments.length == 1) // Means second parameter is not passed
    {
      b = 2;
    }
    return a + b;
}
console.log(add(5,10));
// expected output: 15
console.log(add(5));
// expected output: 7
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Using the Logical OR operator (‘||’)
&lt;/h2&gt;

&lt;p&gt;In this method, the optional parameter is "Logically ORed" with the default value within the body of the function. In the example below, if the value of b is undefined, 2 is passed instead.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//optional parameters Javascript-Using the Logical OR operator (‘||’)
function add(a, b) {
   var b1 = b || 2;
   return a + b1;
}
add(5,10);
// expected output: 15
add(5);
// expected output: 7
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  &lt;span id="section5"&gt;Caveats &amp;amp; References&lt;/span&gt;
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;Checkout &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Default_parameters"&gt;MDN's JavaScript default parameters doc&lt;/a&gt; for more nuances &amp;amp; limitations&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.w3schools.com/js/js_es6.asp"&gt;ECMAScript 2015&lt;/a&gt; allows default parameter values in the function declaration&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>codenewbie</category>
      <category>tutorial</category>
      <category>parameter</category>
    </item>
    <item>
      <title>Implementing Bubble Sort in Javascript</title>
      <dc:creator>tutorials-kept-simple</dc:creator>
      <pubDate>Thu, 29 Oct 2020 10:14:07 +0000</pubDate>
      <link>https://dev.to/tutorialskeptsimple/implementing-bubble-sort-in-javascript-1kko</link>
      <guid>https://dev.to/tutorialskeptsimple/implementing-bubble-sort-in-javascript-1kko</guid>
      <description>&lt;p&gt;In this article, we cover breakdown Bubble Sort and then also share its implementation in Javascript.&lt;/p&gt;

&lt;p&gt;Firstly, let's get it out of the way. When we say "sort", the idea is to re-arrange the elements such that they are in ascending order.&lt;/p&gt;

&lt;p&gt;If you are new to the concept of sorting, each section of the article would be useful - concept of bubble sort, its algorithms, efficiency, etc. However, If you are here to refresh your knowledge, jump straight to the javascript implementation of the sort. &lt;/p&gt;

&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Explanation of Bubble Sort&lt;/li&gt;
&lt;li&gt;Code - Implementing Bubble Sort using Javascript&lt;/li&gt;
&lt;li&gt;Visualization&lt;/li&gt;
&lt;li&gt;Complexity of Bubble Sort&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;span id="section1"&gt;Explanation of Bubble Sort&lt;/span&gt;
&lt;/h2&gt;

&lt;p&gt;If you are a newbie to sorting, Bubble sort is a great place to start! It is one of the more intuitive sorting methods as its algorithm mirrors how our brain generally thinks about sorting - by comparing.&lt;/p&gt;

&lt;p&gt;Let's remove the vagueness and delve deeper into it.&lt;/p&gt;

&lt;h3&gt;
  
  
  A. What Bubble Sort Does?
&lt;/h3&gt;

&lt;p&gt;To achieve sorting in Bubble Sort, the adjacent elements in the array are compared and the positions are swapped if the first element is greater than the second. In this fashion, the largest value "bubbles" to the top.&lt;/p&gt;

&lt;p&gt;Usually, after each iteration the elements furthest to the right are in correct order. The process is repeated until all the elements are in their right position.&lt;/p&gt;

&lt;h3&gt;
  
  
  B. What Bubble Sort Does?
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Starting with the first element, compare the current element with the next element of the array.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If the current element is greater than the next element of the array, swap them.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If the current element is less than the next element, just move to the next element.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Start again from Step 1.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  C. Illustrating the Bubble sort method
&lt;/h3&gt;

&lt;p&gt;Iteration 1: [6,4,2,5,7] → [4,6,2,5,7] → [4,2,6,5,7] → [4,2,5,6,7] → [4,2,5,6,7]&lt;/p&gt;

&lt;p&gt;Iteration 2:[4,2,5,6,7] → [2,4,5,6,7] → [2,4,5,6,7] → [2,4,5,6,7] → [2,4,5,6,7]&lt;/p&gt;

&lt;p&gt;Iteration 3: [2,4,5,6,7] → [2,4,5,6,7] → [2,4,5,6,7] → [2,4,5,6,7] → [2,4,5,6,7]&lt;/p&gt;

&lt;h2&gt;
  
  
  Other Alternatives
&lt;/h2&gt;

&lt;p&gt;As you might have noticed, Bubble Sort only considers one element at a time. Thus, it is highly time consuming and inefficient. Due to its inefficiency, bubble sort is almost never used in production code.&lt;/p&gt;

&lt;p&gt;You can use a built in function Array.prototype.sort() for sorting. This is an inplace algorithm just like bubble sort which converts the elements of the input array into strings and compares them based on their UTF-16 code unit values. Also, if you are interested, you can read about index sort which is another simple comparison based sort method that has a better performance than Bubble sort.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;span id="section2"&gt;Implementing Bubble Sort using Javascript&lt;/span&gt;
&lt;/h2&gt;

&lt;p&gt;Now as we have seen the logic behind bubble sort, we can write the code for it in a straightforward manner, using two nested loops.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let bubbleSort = (inputArr) =&amp;gt; {
    let len = inputArr.length;
    for (let i = 0; i &amp;lt; len; i++) {
        for (let j = 0; j &amp;lt; len; j++) {
            if (inputArr[j] &amp;gt; inputArr[j + 1]) {
                let tmp = inputArr[j];
                inputArr[j] = inputArr[j + 1];
                inputArr[j + 1] = tmp;
            }
        }
    }
    return inputArr;
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see here, the sorting function will run until the variable "i" is equal to the length of the array. This might not be the most efficient solution as it means the function will run on an already sorted array more than once.&lt;/p&gt;

&lt;p&gt;A slightly better solution involves tracking a variable called "checked" which is initially set to FALSE and becomes true when there is a swap during the iteration. Running this code on a do while loop to run the sorting function only when "checked" is true ensures that the function will not run on a sorted array more than once.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let bubbleSort = (inputArr) =&amp;gt; {
    let len = inputArr.length;
    let checked;
    do {
        checked = false;
        for (let i = 0; i &amp;lt; len; i++) {
            if (inputArr[i] &amp;gt; inputArr[i + 1]) {
                let tmp = inputArr[i];
                inputArr[i] = inputArr[i + 1];
                inputArr[i + 1] = tmp;
                checked = true;
            }
        }
    } while (checked);
    return inputArr;
 };
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;span id="section3"&gt;Visualization&lt;/span&gt;
&lt;/h2&gt;

&lt;p&gt;If you are finding it hard to visualize Bubble Sort, you can check this website &lt;a href="https://visualgo.net/bn/sorting?slide=1"&gt;https://visualgo.net/bn/sorting?slide=1&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;You can play around with the code and see the specific function of each part of the code and how they play together to get the final sorted array.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;span id="section4"&gt;Complexity of Bubble Sort&lt;/span&gt;
&lt;/h2&gt;

&lt;p&gt;The worst case scenario: quadratic O(n²): this is the case when every element of the input array is exactly opposite of the sorted order.&lt;/p&gt;

&lt;p&gt;Best case scenario: linear O(n): when the input array is already sorted. Even in this case, we have to iterate through each set of numbers once.&lt;/p&gt;

&lt;p&gt;The space complexity of Bubble Sort is O(1).&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>sort</category>
      <category>codenewbie</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Average or Arithmetic mean of an array using Javascript</title>
      <dc:creator>tutorials-kept-simple</dc:creator>
      <pubDate>Wed, 28 Oct 2020 15:32:57 +0000</pubDate>
      <link>https://dev.to/tutorialskeptsimple/average-or-arithmetic-mean-of-an-array-using-javascript-1k26</link>
      <guid>https://dev.to/tutorialskeptsimple/average-or-arithmetic-mean-of-an-array-using-javascript-1k26</guid>
      <description>&lt;p&gt;The goal of this article is to calculate the average of an array using JavaScript. Before we do that, let’s quickly understand what the terms ‘Average’ &amp;amp; ‘Array’ mean.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Average&lt;/strong&gt; or &lt;strong&gt;Arithmetic mean&lt;/strong&gt; is a representation of a set of numbers by a single number. Its value can be obtained by calculating the sum of all the values in a set and dividing the sum by the number of values.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;Consider the following set of numbers: 1, 2, 3 &amp;amp; 4&lt;br&gt;
Average/Mean  = (1+2+3+4)/4&lt;/p&gt;

&lt;p&gt;An &lt;strong&gt;array&lt;/strong&gt; is a container object that holds a fixed number of values of a single type. An array’s length, once created, would remain constant/fixed.&lt;/p&gt;

&lt;p&gt;You can go through other basic concepts of object-oriented programming such as looping, conditional statements, user-defined functions, and classes to understand this blog better.&lt;/p&gt;

&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Explanation&lt;/li&gt;
&lt;li&gt;Code - Getting average of an array using JavaScript&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;span id="section 1"&gt;Explanation&lt;/span&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Simple approach to finding the average of an array
&lt;/h3&gt;

&lt;p&gt;We would first count the total number of elements in an array followed by calculating the sum of these elements and then dividing the obtained sum by the total number of values to get the Average / Arithmetic mean&lt;/p&gt;

&lt;h2&gt;
  
  
  Breaking down the array average algorithm
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Mean of an array can be obtained in 3 steps:
&lt;/h3&gt;

&lt;p&gt;Step 1: Finding the total number of elements in an array (basically, its length)&lt;br&gt;
This can be obtained by calculating the length of the array using the length method.&lt;/p&gt;

&lt;p&gt;Step 2: Finding the sum of all the elements of an array (sum)&lt;br&gt;
We would need to traverse the array to find the sum. We initialize a variable called ‘total’ and loop over the array and add each element of the array to the ‘total’ variable&lt;/p&gt;

&lt;p&gt;Step 3: Dividing the values obtained in Step 1 &amp;amp; 2.(sum/length)&lt;/p&gt;

&lt;h2&gt;
  
  
  &amp;lt;&lt;span id="section 2"&gt;&amp;gt;Code - Getting average of an array using JavaScript&lt;/span&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Avg{
    constructor(){}

    static average(array){
        var total = 0;
        var count = 0;

        jQuery.each(array, function(index, value){
            total += value;
            count++;
        });

        return total / count;
    }
}

var arry = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];
console.log(Avg.average(arry));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Alternate Methods
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Using Foreach loop
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;arry = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];

function calculateAverage(array){
    var total = 0;
    var count = 0;

    array.forEach(function(item, index){
        total += item;
        count++;
    });

    return total / count;
}

console.log(calculateAverage(arry));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Using jQuery
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var arry = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];
var total = 0;
var count = 0;

jQuery.each(arry , function(index, value){
    total += value;
    count++;
});

console.log(total / count);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Using a function
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var arry = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];

function calculateAverageOfArray(array){
    var total = 0;
    var count = 0;

    jQuery.each(arry , function(index, value)
    {
        total += value;
        count++;
    });

    return total/count;
}

console.log(calculateAverageOfArray(arry));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Using a class
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Avg{
    constructor(){}

    static average(array){
        var total = 0;
        var count = 0;

        jQuery.each(array, function(index, value){
            total += value;
            count++;
        });

        return total / count;
    }
}

var arry = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];
console.log(Avg.average(arry));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>javascript</category>
      <category>average</category>
      <category>codenewbie</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
