Table of contents
- What is JavaScript?
- What is Array?
- How to declare an array?
- What is an index of an array?
- How to print specific values from an array?
- How to add a new value at the last index of an array?
- How to print multiple values from an array of specific indexes?
- How to add the value at a specific index?
- Concatenation:
- How to add and remove values from an array?
- How to check the specific value an array contains?
- How to find an index of a specific value in an array?
- How to check the array?
- How to add data between every element of an array?
- How to reverse the array?
- How to remove the last value from an array?
- How to remove the first value from an array?
- How to add multiple values at starting of an array?
- How to convert any data to an array?
- Important methods of an array:
- How to arrange elements in an ascending order?
What is JavaScript?
Javascript is the most famous programming or scripting language. It allows you to implement complex features on a webpage. enables you to create dynamically updating content, and control multimedia and animated images.
It is developed in 1995. At that time its name was Mocha. In 1996, the name is converted into Live script. After that, till today its name is JavaScript.
What is Array?
Array in javascript is non-primitive. Some features of Array in JavaScript are defined below.
JavaScript arrays are resizable.
It contains a mix of different types of data types.
JavaScript arrays are zero-indexed arrays.
JavaScript arrays are not associative arrays. It means you can only store values that can be retrieved using its index.
How to declare an array?
There are two methods for declaring an array. Both are defined below.
What is an index of an array?
The index is the position of a particular value in the array. It is a unique symbolic name. It always starts with 0. The index of the last value in an array is (length-1).
Ex. let num=[1,2,3,4,5];
Index of 1-0
Index of 2-1
Index of 3-2
Index of 4-3
Index of 5-4
As shown here the index of value 1 is 0, and the index of value 5 is 4. This way, the array is designed.
How to print specific values from an array?
Syntax:
name of array[index];
As shown in the image, a num array is defined. If there is a need to print a specific value from the given index, this method is used.
How to add a new value at the last index of an array?
To add a new value at the last index of an array, the push() method is used. The example is given below.
Here, value'6' is pushed to the array, The value that one wants to add needs to be passed in the parameter form.
How to print multiple values from an array of specific indexes?
To print multiple values from an array, the slice() method is used. Here, we need to add starting and ending index of the array and it will print values between these indexes. The example is shown below.
As shown in the example, in the slice method two indexes are passed which are 1 and 3. It means it prints the values between the 1st to 3rd index. but it excludes the last index which is 3 here and prints the value of indexes 1 and 2, which are values '2' and value '3'. This way the slice() method works.
How to add the value at a specific index?
To add the value at a specific index, the splice() method is used. The example is given below.
As shown in the example, by using this method one can add and remove values from an array.
The first parameter represents the index, where you want to insert data. Here it is 2.
The second parameter represents the number of values one wants to remove after that index. Here it is 1, which means after the second index, one value will be removed. Here, it removes the value '3'.
At the third parameter, one needs to add values that one wants to add to the array. Here are value'6' and value '7'. One can add single or multiple values by the third parameter.
Concatenation:
Concatenation means connecting or joining two strings together. The example is shown below. As the name, suggest for concatenation, the concat () method is used. The array one wants to concat needs to be passed in the parameter.
As shown in the example, it connects two arrays and displays them combined.
One can also combine three arrays, which are shown below. In this situation, there are two arrays passed in the parameter separated by a comma.
Even if, the length of arrays is different, arrays can be combined.
How to add and remove values from an array?
For doing this combined, the fill () method is used. The example is shown below.
The first parameter which is passed in the method is the value one wants to add. Here it is 10.
The second parameter is the index where one wants to store value. Here it is 2. So the value started adding at the second index of the array.
The third parameter represents the last index, up to which index one wants to repeat that value. Here, it is 4. But the 4th index is excluded and up to index 3 value 10 is added and values '3' and '4' values are removed from an array.
How to check the specific value an array contains?
To check whether the value is there or not in the array, the include() method is used.
It checks the value at the specific index. In this method, two parameters are passed. The first parameter is the index on which a search is required. The second parameter is the value that needs to be checked
Here, it checks whether the value '4' is there or not at index 3. It gives output in a boolean value.
How to find an index of a specific value in an array?
To find the index, the indexOf() method is used.
In the parameter value is passed for which there is a need to find the index. As shown in the example, an index of 4 is found by using this method. This way one can find the index of any value from an array.
How to check the array?
If a group of data is given, it is difficult to identify whether it is an array or not. To resolve this issue, the isArray() method is used.
The isArray() method is accessible by the Array object. In the parameter of the method name of an array is passed.
It returns the boolean output. Here, num is an array, that is why it returns true.
How to add data between every element of an array?
To add any data between every element of an array, the join() method is used. The example is shown below.
As shown in the example, space with the "and" keyword is passed as a parameter in the method join(). Updated array added ' and ' keyword between every element of array and datatype of that array also converts in an object. This way space and commas also can be added.
How to reverse the array?
As per the purpose, to reverse an array the reverse() method is used. The example is shown below.
As shown in the example, the method gives reversed array in the output. The last value comes at zero indexes and the value from zero index shifts to the last value of an array.
How to remove the last value from an array?
The pop() method is used to remove the last value from an array. The example is shown below.
As shown in the example, the method removes the last value from an array. Here, the last value is 5. So, value '5' is removed. After the application of the pop() method, the array is printed, which does not display the value '5'.
How to remove the first value from an array?
To remove the first value from an array, the shift() method is used. The example is below.
As shown in the image, the shift() method removes value'1' from an array. After the application of the shift() method, the array is printed, which does not display the value '1' at index 0. In short, this method removes value from an index '0'.
How to add multiple values at starting of an array?
To add multiple values at starting of an array, the unshift() method is used. The example is given below.
In this method, multiple parameters can be passed. Parameters include values that need to be added.
As shown in the example, two parameters are passed in the method, which are fruit1 and fruit2. The method gives the length of an array in an output. After the application of the method, the printed array shows the array with new values.
How to convert any data to an array?
To convert given data in an array, the split()method is used.
Here, there is a string given. After applying the split() method, it is stored in an array. When one displays that array, it converts into separate elements and displays in the form of an array.
Important methods of an array:
1) lastIndexOf():
When there is the repetition of single data in an array and there is a need to find the last index of that repetitive data, at that time this method is used.
The method is called by the object, which is the name of an array. In the parameter value is passed for which there is a need to find the last index.
As shown in the example, here value'1' is repeating four times in the array and if there is a need to find the last index of value'1', this method is used. Here, it gives 9, which is the index of the last value'1'.
2) map():
When there is a need to apply one method to each element of an array, at that time the map() method is used. It iterates over an array and modifies its elements using a callback function.
Here, there is a need to find the square root of every element of the array.So, Math. sqrt() is passed in the parameter. It iterates to every element of an array and applies the method Math. sqrt() and returns its square root in the form of an array.
How to arrange elements in an ascending order?
The sort() method is used to arrange data in ascending order. The example is given below.
As shown in the example, random data is added, After applying the sort() method, it covers all elements in ascending order.
3) For-of method:
The for-of statement executes a loop that operates on a sequence of values sourced from an iterable object.
As shown in the example, one blank array is created which is an upper letter. In a for-of loop, the object of the original array is generated, which is upper. By using that object, every element of an original array is accessible, which is converted into upper case and pushed into an upper letter named array. By printing that array one can see that all the elements are in uppercase.
Here for-of is used for converting data into uppercase. This way for another purpose also for-of can be used.