In JavaScript, arrays consist only of values
-
Array literal
var a = [1,2,3]
-
Array constructor function
var b = new Array(1,2,3)
-
forvar a = [1,2,3] for (var i=0 ; i <a.length; i++){ console.log(i, a[i]) }
-
for .. of: Outputs element values one by one
for (var elem of a){ console.log(elem) }
-
forEach: Takes a function itself as an argument
a.forEach( function(elem, idx) { console.log(idx, elem) })
-
for .. in- Not only accesses array elements but properties can also be output
- Since arrays in JavaScript are also objects, property settings are possible, but they become properties of the Object, not the list
var a = [1,2,3] a.name = 'myarr' for (var i in a){ console.log(i, a[i]) } //0 1 //1 2 //2 3 //name myarr => iterates through properties as well
- Be careful when using the for ... in form!
- for ...in iterates through all properties of the Object itself
- Not only accesses array elements but properties can also be output
sort- If there is no comparison function (argument), it sorts based on strings
- If you don't want that, pass a comparison function as an argument
- If there is a comparison function, it sorts based on the return value being less than 0
- If there is no comparison function (argument), it sorts based on strings
-
String-related
join- array.join('haha')
-
toString -
Array merging
-
concat: Merges two arrays
-
-
Element insertion/deletion
pushpopunshift- Insert at the left end
shift- Remove from the left end
-
Index search
indexOf
-
Array manipulation
-
splice(start[, deleteCount[, item1[, item2[, ...]]]])-
Modifies the original array itself
-
Can also modify/delete elements
var a = [1,2,3] a.splice(1,2, "first", "second") // Delete two from position 1 + append "first", "second" // [1, "first", "second"] var a = [1,2,3] a.splice(1,2, "first") // Delete two from position 1 + append "first" // [1, "first"]
-
-
-
Array slicing
-
slice: Returns the result
-
function sum(a,b) {
return a+b;
}
sum(1,2) //3// Assign an anonymous function to a variable
var sub = function(a,b) {
return a - b;
}
sub(1,2) //-1(function(a,b){return a*b})(1,2) //2var sum = (a,b) => a+b // Single line a+b so curly braces {} are omitted
sum(3,4) //7
var area = (r) => {
const PI = 3.14;
return r*r*PI;
}
area(1) //3.14-
In JavaScript, there are no restrictions on passing parameters to functions
-
The
argumentsobject contains all information about the parameters passedfunction foo(a) { console.log(arguments); // Print arguments to check return a }
-
If no arguments are passed, it shows undefined
-
undefined: The value JavaScript assigns when initializing a variable
-
-