that should make you think of an array.
flexible. An array can hold many values under a single name, and you can access the values by referring to an index number. Of
array is that you do not have to Specify the length. When you create
one, you just start putting in your elements.
can always expand joyously unlike java that once you have specified the
length at creation, sorry You can’t go beyond that. Hence, you have an
array index out of bounds exception.
Also, Know that you can always create an array and leave it for later use, it is simply by doing this:
- The join() method also joins all array elements into a string.
- The pop() method returns the value that was “popped out”
- The push() method adds a new element to an array (at the end). When you work with arrays, it is easy to remove elements and add new elements.
- The shift() method removes the first array element and “shifts” all other elements to a lower index and returns the value that was “shifted out”.
- The unshift() method adds a new element to an array (at the beginning), and “unshifts” older elements, and returns the new array length.
- The splice() method can be used to add new items to an array, specifying what index you want to add the new items too.
- The concat() method creates a new array by merging (concatenating) existing arrays,
Note that The concat() method does not change the existing arrays. It
always returns a new array. The concat() method can take any number of
work, we would be eliminating empty elements from the array. There are
several code implementations provided for this, which means you have the
opportunity to learn different ways of doing it, and even more than that,
only if you could come up with your own
1. IMPLEMENTATION 1
In Line 1 an array variable was created with values initialized directly. Line
2 created a variable that holds a function “array.filter” which
traverses through each element (el) and returns each element if it is
not null and not empty.
Line 5 prints the variable that was created which holds an array.filter() method.
[ 1, 2, 2, 4, 4, 5, 6 ]
2. IMPLEMENTATION 2
code implementation 2, An array was populated in line 2 with a different
set of values, integers, string, null, undefined and empty. A new empty
array is created in line 5 for later use and line 10 runs a for loop,
line 11 means if the value at each index is true, meaning if it contains a
value then it should push it to the new array that was created which was
empty. And line 15 should print the new array in the console.
Implementation 3 is another way of doing the same thing, line 1
declared some set of values in an array. And in line 2 was a variable
called filtered that was created to hold the method array.filter.
call to do any filtering task for yourself. The good news is , you don’t have
to start writing this on your own, It is already there. So, the
array.filter() took in Boolean.
What this is saying is that it should
bring out elements that are true in the array. By so doing, the empty
elements are automatically discarded in the array. So, we only have the
following as an output.
may be wondering why zero was not present in the output, It Can’t!
Because of the approach we used. A predicate was passed into the
parenthesis and as far as it is concerned Zero is false too. Not just
the null, empty and undefined.