JavaScript – codewindow.in

Related Topics

JAVASCRIPT

Here’s an example of declaring an array named numbers that contains some integer values:
You can also create an empty array and then add elements to it later:
Once you have declared an array, you can access its elements using their index. Array indices start from 0, so the first element is at index 0, the second element is at index 1, and so on. You can access an element by using square brackets and providing the index:
You can also modify the elements of an array by assigning new values to specific indices:
Arrays in JavaScript are dynamic, which means you can add or remove elements at any time using various built-in methods and properties.
You can also push multiple elements at once:
2. Unshift: The unshift() method adds one or more elements to the beginning of an array and returns the new length of the array.
You can also unshift multiple elements at once:
3. Concat: The concat() method is used to merge two or more arrays. It does not modify the original array but returns a new array containing the combined elements.
5. Splice: The splice() method allows you to add elements at a specific index position within an array. It can also be used to remove elements. The splice() method modifies the original array and returns an array containing the removed elements (if any).
In the example above, splice(3, 0, 4, 5) inserts the elements 4 and 5 at index 3 of the numbers array.
These are just a few ways to add elements to an array in JavaScript. The method you choose depends on your specific use case and requirements.
In the example above, splice(2, 1) removes one element starting from index 2 in the numbers array.
2. pop(): The pop() method removes the last element from an array and returns that element.
The pop() method modifies the original array by removing the last element.
3.shift(): The shift() method removes the first element from an array and returns that element. All subsequent elements are shifted down to lower indexes.
Similarly to pop(), shift() modifies the original array.
4.filter(): The filter() method creates a new array with all elements that pass a test provided by a callback function. You can use it to effectively remove specific elements based on a condition.
In the example above, filter() creates a new array excluding the element equal to 3.
These methods offer different approaches to removing elements from an array in JavaScript. Choose the method that suits your specific requirements and the nature of the elements you want to remove.
splice():
  • splice() is used to modify an array by adding, removing, or replacing elements.
  • It takes three or more parameters: start, deleteCount, and optional item1, item2, … parameters to add elements.
  • The start parameter specifies the index at which to start the modification. The deleteCount parameter specifies the number of elements to remove.
  • Elements can be added at the start index by specifying them as additional parameters to splice().
  • The splice() method modifies the original array and returns an array containing the removed elements (if any).
  • Negative indices can be used to start counting from the end of the array.
Example:
In summary, slice() creates a new array by extracting elements from an existing array, while splice() modifies the original array by adding, removing, or replacing elements at a specific index.
The sort() method sorts the fruits array alphabetically.
If you have an array of numbers, the sort() method performs lexicographic sorting by default, which might not produce the desired result. To sort an array of numbers in ascending order, you need to provide a compare function as an argument to sort():
In this example, the provided compare function subtracts b from a, resulting in ascending numerical sorting.
If you want to sort the array in descending order, you can reverse the order of subtraction in the compare function:
By customizing the compare function, you can achieve different sorting behaviors based on your specific requirements.
In the example above, the spread syntax (...) is used to spread the elements of the numbers array as individual arguments to the Math.min() and Math.max() functions.
Using reduce():
In this example, the reduce() method is used to iterate over the numbers array and continuously reduce it to a single value by comparing each element with the accumulated value (a). The Math.min() and Math.max() functions are used to determine the minimum and maximum values, respectively.
Both approaches allow you to find the minimum or maximum value in an array. Choose the method that suits your preference and coding style.

      

Popular Category

Topics for You

Go through our study material. Your Job is awaiting.

Categories