10 common Array methods in JS

var arr = ["a","b","c"];

arr.push("d");
//just adds a new element into the end of an array
console.log(arr); // ["a","b","c","d"]

//arr.pop();
//it removes the element in an array and returns that element
console.log(arr.pop()); // "d"
console.log(arr); // ["a","b","c"]

//concat()
//concatenates two arrays
var arr2 = ["g","q"];
console.log(arr.concat(arr2)); // ["a","b","c","d","g","q"]

//join()
console.log(arr.join("")); // "abc"
console.log(arr); //["a","b","c"]

//reverse()
console.log(arr.reverse()); //["c","b","a"]
console.log(arr); //["c","b","a"]

//shift()
//it's going to remove the first element in the array and return that element
console.log(arr.shift()); // "c"
console.log(arr); //["b","a"]

//unshift()
//it adds a new element to the beginning of your array and returns the length of the modified array
console.log(arr.unshift("p")); // 3
console.log(arr); //["p","b","a"]

//slice()
console.log(arr.slice(1,2)); // ["b"]
console.log(arr.slice(1,3)); // ["b","a"]
console.log(arr.slice(1,10)); // ["b","a"]
console.log(arr); // ["p","b","a"]

//sort()
console.log(arr.sort()); // ["a","b","p"]
console.log(arr); // ["a","b","p"]

//splice()
//it's going to modify the original array instead of just returning a new array
arr.push("g");
arr.push("w");
console.log(arr); // ["a","b","p","g","w"]
console.log(arr.splice(2,2,"JS Nuggets")); // ["p","g"]
console.log(arr); // ["a","b","JS Nuggets","w"]


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值