Pages

Javascript Array join() Method

4/7/14

Description:

Javascript array join() method joins all elements of an array into a string.

Syntax:

array.join(separator);
Here is the detail of parameters:
  • separator : Specifies a string to separate each element of the array. If omitted, the array elements are separated with a comma.

Return Value:

Returns a string after joining all the array elements.

Example:

<html>
<head>
<title>JavaScript Array join Method</title>
</head>
<body>
<script type="text/javascript">

var arr = new Array("First","Second","Third");

var str = arr.join();
document.write("str : " + str ); 

var str = arr.join(", ");
document.write("<br />str : " + str ); 

var str = arr.join(" + ");
document.write("<br />str : " + str ); 
</script>
</body>
</html>
This will produce following result:
str : First,Second,Third
str : First, Second, Third
str : First + Second + Third  
Read more ...

Javascript Array indexOf() Method

4/7/14

Description:

Javascript array indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present.

Syntax:

array.indexOf(searchElement[, fromIndex]);
Here is the detail of parameters:
  • searchElement : Element to locate in the array.
  • fromIndex : The index at which to begin the search. Defaults to 0, i.e. the whole array will be searched. If the index is greater than or equal to the length of the array, -1 is returned.

Return Value:

Returns the index of the found element.

Compatibility:

This method is a JavaScript extension to the ECMA-262 standard; as such it may not be present in other implementations of the standard. To make it work you need to add following code at the top of your script:
if (!Array.prototype.indexOf)
{
  Array.prototype.indexOf = function(elt /*, from*/)
  {
    var len = this.length;

    var from = Number(arguments[1]) || 0;
    from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
    if (from < 0)
      from += len;

    for (; from < len; from++)
    {
      if (from in this &&
          this[from] === elt)
        return from;
    }
    return -1;
  };
}

Example:

<html>
<head>
<title>JavaScript Array indexOf Method</title>
</head>
<body>
<script type="text/javascript">
if (!Array.prototype.indexOf)
{
  Array.prototype.indexOf = function(elt /*, from*/)
  {
    var len = this.length;

    var from = Number(arguments[1]) || 0;
    from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
    if (from < 0)
      from += len;

    for (; from < len; from++)
    {
      if (from in this &&
          this[from] === elt)
        return from;
    }
    return -1;
  };
}
var index = [12, 5, 8, 130, 44].indexOf(8);
document.write("index is : " + index ); 

var index = [12, 5, 8, 130, 44].indexOf(13);
document.write("<br />index is : " + index ); 
</script>
</body>
</html>
This will produce following result:
index is : 2
index is : -1 
Read more ...

Javascript Array forEach() Method

4/7/14

Description:

Javascript array forEach() method calls a function for each element in the array.

Syntax:

array.forEach(callback[, thisObject]);
Here is the detail of parameters:
  • callback : Function to test each element of the array.
  • thisObject : Object to use as this when executing callback.

Return Value:

Returns created array.

Compatibility:

This method is a JavaScript extension to the ECMA-262 standard; as such it may not be present in other implementations of the standard. To make it work you need to add following code at the top of your script:
if (!Array.prototype.forEach)
{
  Array.prototype.forEach = function(fun /*, thisp*/)
  {
    var len = this.length;
    if (typeof fun != "function")
      throw new TypeError();

    var thisp = arguments[1];
    for (var i = 0; i < len; i++)
    {
      if (i in this)
        fun.call(thisp, this[i], i, this);
    }
  };
}

Example:

<html>
<head>
<title>JavaScript Array forEach Method</title>
</head>
<body>
<script type="text/javascript">
if (!Array.prototype.forEach)
{
  Array.prototype.forEach = function(fun /*, thisp*/)
  {
    var len = this.length;
    if (typeof fun != "function")
      throw new TypeError();

    var thisp = arguments[1];
    for (var i = 0; i < len; i++)
    {
      if (i in this)
        fun.call(thisp, this[i], i, this);
    }
  };
}

function printBr(element, index, array) {
  document.write("<br />[" + index + "] is " + element ); 
}

[12, 5, 8, 130, 44].forEach(printBr);
  
</script>
</body>
</html>
This will produce following result:
[0] is 12
[1] is 5
[2] is 8
[3] is 130
[4] is 44 
Read more ...

Javascript Array filter() Method

4/7/14

Description:

Javascript array filter() method creates a new array with all elements that pass the test implemented by the provided function.

Syntax:

array.filter(callback[, thisObject]);
Here is the detail of parameters:
  • callback : Function to test each element of the array.
  • thisObject : Object to use as this when executing callback.

Return Value:

Returns created array.

Compatibility:

This method is a JavaScript extension to the ECMA-262 standard; as such it may not be present in other implementations of the standard. To make it work you need to add following code at the top of your script:
if (!Array.prototype.filter)
{
  Array.prototype.filter = function(fun /*, thisp*/)
  {
    var len = this.length;
    if (typeof fun != "function")
      throw new TypeError();

    var res = new Array();
    var thisp = arguments[1];
    for (var i = 0; i < len; i++)
    {
      if (i in this)
      {
        var val = this[i]; // in case fun mutates this
        if (fun.call(thisp, val, i, this))
          res.push(val);
      }
    }

    return res;
  };
}

Example:

<html>
<head>
<title>JavaScript Array filter Method</title>
</head>
<body>
<script type="text/javascript">
if (!Array.prototype.filter)
{
  Array.prototype.filter = function(fun /*, thisp*/)
  {
    var len = this.length;
    if (typeof fun != "function")
      throw new TypeError();

    var res = new Array();
    var thisp = arguments[1];
    for (var i = 0; i < len; i++)
    {
      if (i in this)
      {
        var val = this[i]; // in case fun mutates this
        if (fun.call(thisp, val, i, this))
          res.push(val);
      }
    }

    return res;
  };
}

function isBigEnough(element, index, array) {
  return (element >= 10);
}

var filtered  = [12, 5, 8, 130, 44].filter(isBigEnough);
document.write("Filtered Value : " + filtered ); 
  
</script>
</body>
</html>
This will produce following result:
Filtered Value : 12,130,44 
Read more ...

Javascript Array every Method

4/7/14

Description:

Javascript array every method tests whether all elements in the array pass the test implemented by the provided function.

Syntax:

array.every(callback[, thisObject]);
Here is the detail of parameters:
  • callback : Function to test for each element.
  • thisObject : Object to use as this when executing callback.

Return Value:

Returns true if every element in this array satisfies the provided testing function.

Compatibility:

This method is a JavaScript extension to the ECMA-262 standard; as such it may not be present in other implementations of the standard. To make it work you need to add following code at the top of your script:
if (!Array.prototype.every)
{
  Array.prototype.every = function(fun /*, thisp*/)
  {
    var len = this.length;
    if (typeof fun != "function")
      throw new TypeError();

    var thisp = arguments[1];
    for (var i = 0; i < len; i++)
    {
      if (i in this &&
          !fun.call(thisp, this[i], i, this))
        return false;
    }

    return true;
  };
}

Example:

<html>
<head>
<title>JavaScript Array every Method</title>
</head>
<body>
<script type="text/javascript">
if (!Array.prototype.every)
{
  Array.prototype.every = function(fun /*, thisp*/)
  {
    var len = this.length;
    if (typeof fun != "function")
      throw new TypeError();

    var thisp = arguments[1];
    for (var i = 0; i < len; i++)
    {
      if (i in this &&
          !fun.call(thisp, this[i], i, this))
        return false;
    }

    return true;
  };
}
function isBigEnough(element, index, array) {
  return (element >= 10);
}

var passed = [12, 5, 8, 130, 44].every(isBigEnough);
document.write("First Test Value : " + passed ); 
  
passed = [12, 54, 18, 130, 44].every(isBigEnough);
document.write("Second Test Value : " + passed ); 
</script>
</body>
</html>
This will produce following result:
First Test Value : falseSecond Test Value : true
Read more ...

Javascript Array concat() Method

4/7/14

Description:

Javascript array concat() method returns a new array comprised of this array joined with two or more arrays.

Syntax:

array.concat(value1, value2, ..., valueN);
Here is the detail of parameters:
  • valueN : Arrays and/or values to concatenate to the resulting array.

Return Value:

Returns the length of the array.

Example:

<html>
<head>
<title>JavaScript Array concat Method</title>
</head>
<body>
<script type="text/javascript">
   var alpha = ["a", "b", "c"];
   var numeric = [1, 2, 3];

   var alphaNumeric = alpha.concat(numeric);
   document.write("alphaNumeric : " + alphaNumeric ); 
</script>
</body>
</html>
This will produce following result:
alphaNumeric : a,b,c,1,2,3 
Read more ...

Javascript String - sup() Method

4/7/14

Description:

This method causes a string to be displayed as a superscript, as if it were in a <sup> tag.

Syntax:

string.sup( )
Here is the detail of parameters:
  • NA

Return Value:

  • Returns the string with <sup> tag.

Example:

<html>
<head>
<title>JavaScript String sup() Method</title>
</head>
<body>
<script type="text/javascript">

var str = new String("Hello world");
alert(str.sup());

</script>
</body>
</html>
This will produce following result:
<sup>Hello world</sup>
Read more ...

Javascript String - sub() Method

4/7/14

Description:

This method causes a string to be displayed as a subscript, as if it were in a <sub> tag.

Syntax:

string.sub( )
Here is the detail of parameters:
  • NA

Return Value:

  • Returns the string with <sub> tag.

Example:

<html>
<head>
<title>JavaScript String sub() Method</title>
</head>
<body>
<script type="text/javascript">

var str = new String("Hello world");
alert(str.sub());

</script>
</body>
</html>
This will produce following result:
<sub>Hello world</sub>
Read more ...

Javascript String - strike() Method

4/7/14

Description:

This method causes a string to be displayed as struck-out text, as if it were in a <strike> tag.

Syntax:

string.strike( )
Here is the detail of parameters:
  • NA

Return Value:

  • Returns the string with <strike> tag.

Example:

<html>
<head>
<title>JavaScript String strike() Method</title>
</head>
<body>
<script type="text/javascript">

var str = new String("Hello world");
alert(str.strike());

</script>
</body>
</html>
This will produce following result:
<strike>Hello world</strike>
Read more ...

Javascript String - small() Method

4/7/14

Description:

This method causes a string to be displayed in a small font, as if it were in a <small> tag.

Syntax:

string.small( )
Here is the detail of parameters:
  • NA

Return Value:

  • Returns the string with <small> tag.

Example:

<html>
<head>
<title>JavaScript String small() Method</title>
</head>
<body>
<script type="text/javascript">

var str = new String("Hello world");
alert(str.small());

</script>
</body>
</html>
This will produce following result:
<small>Hello world</small>
Read more ...

Related Posts