var myArrayLiteral = [1,2,3];
var myArrayConstructed = new Array(1,2,3);
console.log(myArrayLiteral);
console.log(myArrayConstructed);
// stick to [] to access, since arr.0 will fail
console.log(myArrayLiteral[0]);
// Arrays have a length property
console.log(myArrayLiteral.length);
// Arrays have methods to play with
console.log("Is this an array? ", Array.isArray(myArrayLiteral));
var lastEl = myArrayLiteral.pop();
var firstEl = myArrayLiteral.shift();
console.log(firstEl, lastEl, myArrayLiteral);
myArrayLiteral.push(lastEl);
myArrayLiteral.unshift(firstEl);
console.log(myArrayLiteral);
// Iterating over an array
for (var i=0; i < myArrayLiteral.length; i++) {
console.log(myArrayLiteral[i]);
}
myArrayLiteral.forEach(function(el, i, originalArray) {
console.log(el, i, originalArray);
});
var myArrayLiteralIsAllEven = myArrayLiteral.every(function(n) {
return n % 2 === 1;
});
console.log(myArrayLiteralIsAllEven);
var myArrayLiteralHasEven = myArrayLiteral.some(function(n) {
return n % 2 === 1;
});
console.log(myArrayLiteralHasEven);
var myArrayLiteralEvens = myArrayLiteral.filter(function(n) {
return n % 2 === 0;
});
console.log(myArrayLiteralEvens);
var myArrayLiteralRaisedByTwo = myArrayLiteral.map(function(n) {
return Math.pow(n, 2);
});
console.log(myArrayLiteralRaisedByTwo);
var myArrayLiteralReduced = myArrayLiteral.reduce(function(prev, curr) {
return prev + curr;
});
console.log(myArrayLiteralReduced);
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.