List of Some important javascript functions



JavaScript is a lightweight, interpreted programming language with object-oriented capabilities that allows you to build interactivity into otherwise static HTML pages. We all know that today jQuery and Prototype are mostly uses JavaScript library. But we should know about standard JavaScript functions, So here I am listing some important JavaScript functions with their uses.

split() : string.split(separator,limit);
The split() method splits a string into an array of sub-strings and returns the array. If separator is empty, it splits each character of string. limit is used to specify not to include in array.

var str = "Hello, how are you?";
var strArr = str.split(',');

join() : arrayName.join(delimiter);
The join() method joins the elements of an array into a string with specified separator and returns the string. The default separator is comma (,).

[1,2,3,4].join('; ');

substring() : string.substring(start,end)
The substring() method gets the characters between specified indices and returns a new sub string. If “start” is greater than “end”, this method will swap the two arguments, means str.substring(1,4) == str.substring(4,1).

var str = "Hello world!";
var res = str.substr(1,6);

length : string.length
It returns the length of a string i.e. number of characters. 0 for empty string.

var str = "Hello World!";
var n = str.length;

trim() : string.trim();
The trim() method removes white space from both sides of the string.

var str = " Hello World! ";
str = str.trim(); 

toString() : array.toString()
This method converts an array into a String with separate the elements with commas.

var techs = ["Apple", "Android", "Windows"];
techs.toString();
output : Apple,Android,Windows

sort() : arr.sort();
The sort() method sorts elements of an Array in alphabetical order. So, non-string elements are converted into strings before sorting operation, then sorting operation is done. So dealing with number results unexpected outcome.

var techs = ["Apple", "Windows", "Android"];
techs.sort();
document.write(techs.toString());

output : Android,Apple,Windows

getElementById() : document.getElementById(“id”)
The getElementById() method accesses the first element with the specified id.

strpos() strpos (haystack, needle, offset)
It takes the inputs same as in PHP and returns the position of matched character or string.

function strpos (haystack, needle, offset) {
  var i = (haystack+'').indexOf(needle, (offset || 0));
  return i === -1 ? false : i;
}
strpos('Hello Howdy', 'w', 5);

LIVE DEMO

List of Some important javaScript functions
List of Some important javaScript functions