ES6 Array Methods

1. concat() :

concat() method joins two or more methods.

Syntax :

array.concat(val1,val2...valn);

It returns a new array.

Example :

var firstArray = [ 'aa','bb','cc'];
var secondArray = [ 1,2,3 ];
var thirdArray = firstArray.concat(secondArray);
console.log(thirdArray);
output:
['aa','bb','cc',1,2,3];

2.every() :

every() method tests whether all the elements in the array satisfies the condition which is been implemented by the function.

Syntax :

array.every(callback[,thisObject]);

It returns true if every element satisfies the condition.

Example :

function test(element){
return (element > 0 )
}

var check = [1,2,3,4,0].every(test);
console.log(check);

output :
false

3. filter() :

filter() method creates a new array with all the elements which will pass the test.

Syntax :

array.filter(callback[,thisObject]);

It returns the created array.

Example :

function test(element){
return (element > 0 )
}

var check = [1,2,3,4,0].filter(test);
console.log(check);

output :
[1,2,3,4]

4. forEach() :

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

Syntax :

array.forEach(callback[,thisObject]);

It returns the created array.

Example :

var a= new Array(1,2,3,4)
a.forEach(function(val,index){
console.log(val)
})

output :
1
2
3
4

5. indexOf()

indexOf() method returns the first index of the element from the array if it is not present then it returns -1.

Syntax :

array.indexOf(searchelement[,fromIndex]);

Example :
var getIndex = [111, 222, 333, 444 ].indexOf(222);
console.log(getIndex );
output :
1

6. join()

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

Syntax :

array.join(separator);

separator: It specifies a string to separate the each element of the array and if separator is not passed, then the array elements are separated by comma.

It returns the string after joining all the elements in the array.

Example :

var getIndex = [111, 222, 333, 444 ];

var newArr = getIndex.join();
console.log(newArr); 
output :
"111,222,333,444"

var newArr1 = getIndex.join(" - ");
console.log(newArr1); 
output :
"111 - 222 - 333 - 444"

7. lastIndexOf() :

lastIndexOf() method returns the last index of the given element from the array and if the element is not present then it returns -1.The array is searched from backwards.

Syntax :

array.lastIndexOf(searchIndex[,fromIndex]);

Example :

var getLastIndex = [12, 5, 8, 130, 44,45,8,65,99].lastIndexOf(8); 
console.log("index is : " + getLastIndex );   
output :
index is : 6

8. map() :

map() method creates a new array with the given result by calling the function for each and every element.

Syntax :

array.map(callback[,thisObject]);

Example :

var nos = [9,16,25,36,49];
var rootsOfNumbers = nos.map(Math.sqrt);
console.log(rootsOfNumbers );
output :
[3, 4, 5, 6, 7]

9. pop() :

pop() method removes the last element from the array and returns that element.

Syntax :

array.pop();

Example :

var nos = [9,16,25,36,49]; 
var removedElement = nos.pop();
console.log(removedElement );
output : 49
console.log(nos);
[9,16,25,36]

10. push() :

push() method adds the element at the end of the array.

Syntax :

array.push(ele1,ele2,...,elen);

Example :
var nos = [9,16,25,36,49]; 
nos.push(64);
console.log(nos);
output :
[9, 16, 25, 36, 49, 64]

11. reduce() :

reduce() method applies a callback function for the values of the array and reduces it to one from left to right.

Syntax :

array.reduce(callback[,initialValue);

Example :

var sub = [99, 20, 12, 5].reduce(function(a, b){ return a - b; }); 
console.log("number : " + sub );    
output : 
number : 62

12. reduceRight() :

reduceRight() method applies a callback function for the values of the array and reduces it to one from right to left.

Syntax :

array.reduceRight(callback[, initialValue]);    

Example :

var sub = [99, 20, 12, 5].reduceRight(function(a, b){ return a - b; }); 
console.log("number : " + sub );
output : 
number : -126

13. reverse() :

The reverse() method will reverse the element i.e, the last becomes first and first will be last.

Syntax :

array.reverse();

Example :

var a = ["name", "age" , "job" ].reverse();
console.log(a);
output :
["job", "age", "name"]

14. shift() :

shift() method removes the first element from the array.

Syntax :

array.shift();

Example :

var a = ["name", "age" , "job" ];
a.shift();
console.log(a);
output :
["age", "job"]

15. unshift() :

unshift() method adds the one or more elements at the beginning of the array.

Syntax :

array.unshift(ele1,...,eleN);

Example :

var arr = new Array("apple", "banana" , "kiwi");
arr.unshift("watermelon");
console.log(arr);
output:
["watermelon", "apple", "banana", "kiwi"]

16. slice() :

slice() method get the specific element from the array.

Syntax :

array.slice(begin[,end]);

Example :

var arr = new Array("apple", "banana" , "kiwi");
arr.slice(1,2);
output :
["banana"]







JavaScript Page Redirect

Page redirection is way to redirect from one webpage to another webpage. The redirected page can be on the same website or on a different website or web server.

window.location and window.location.href

In JavaScript, we can use many methods to redirect a webpage to another webpage. window.location is a object. It is used to get the current URL address and to redirect the browser to the new page. This property is of window object. The behaviour is same for both window.location and window.location.href.

Example : 

function redirectPage(){
       window.location = "http://www.google.com";
}
 redirectPage();

The page is redirected to google.

location.replace() :

The most frequently used is replace() method in window.location object, replace() method will replace the current document with new url and performs HTTP redirect.

Syntax :

window.location.replace("http://www.kfc.com");

location.assign() :

This method loads a new document in the browser window.

Syntax :

window.location.assign("http://www.kfc.com");

Difference between location.assign() and location.replace() :

location.replace() method deletes the current url from the document history. So, we cannot go back to the original document where as the location.replace() method loads a new document in the browser. To avoid this situation, we need to use location.assign() method.

location.reload() :

The location.reload() method reloads the current document in the browser.

Syntax :

window.location.reload("http://shruthikatkoori.blogspot.com");

window.navigate() :

This is almost similar to window.location.href property i.e, assigning a new value. This is only available in MS Internet Explorer so better to avoid this in cross-browser development.

Syntax :

window.navigate("http://www.abc.com");

Redirection and Search Engine Optimisation :

If you want to notify the url forwarding to the SEO then need to add rel = "canonical" meta tag to your website head part because the search engines don't identify the JavaScript redirection.



Overview of AngularJS

AngularJS is an open source web application framework. It is now maintained by Google . The most of the code can be eliminated by using th...