JavaScript
What is Javascript?
JavaScript (js) is a light-weight object-oriented programming language which is used by several websites for scripting the webpages. It is an interpreted, full-fledged programming language that enables dynamic interactivity on websites when applied to an HTML document.
With JavaScript, users can build modern web applications to interact directly without reloading the page every time. The traditional website uses js to provide several forms of interactivity and simplicity.
now, we understood what is javascript, lets go further and understand in depth about it.
Lets see what is Arrays in Javascript.
Arrays in JavaScript
In JavaScript, array is a single variable that is used to store different elements. It is often used when we want to store list of elements and access them by a single variable.
Declaration of an Array There are basically two ways to declare an array. Example:
var num1= [ ]; // method 1
var num2= new Array(); // method 2
An array in JavaScript can hold different elements We can store Numbers, Strings and Boolean in a single array. Example:
var arr=[1 , true, "chan", null]
Now, lets see the methods of arrays in JavaScript
Converting Arrays to Strings
The JavaScript method toString() converts an array to a string of (comma separated) array values.
const fruits = ["Banana", "Orange", "Apple", "Mango"];
console.log(fruits.toString());
length
The length is a property used to find the length of the array, which means it will fin dhow many elements present in the array.
const arr = [ 10 , 20 , "chan" , "hi" , 40 ];
console.log(arr.length); //it will give the length of array as 5
push()
The push() method adds a new element to an array (at the end).
const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.push("grapes");
pop()
const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.pop();
shift()
The shift() method removes the first array element and "shifts" all other elements to a lower index.
const arr = [ 10 , 20 , "chan" , "hi" , 40 ];
console.log(arr.shift());
unshift()
The unshift() method adds a new element to an array (at the beginning), and "unshifts" older elements.
const arr = [ 10 , 20 , "chan" , "hi" , 40 ];
console.log(arr.unshift("hello"));
at(index)
this method helps to return the value of array at the specific index.
If the specified index cannot be found, returns undefined
const arr = [ 10 , 20 , "chan" , "hi" , 40 ];
console.log(arr.at(2)); //ans is chan
indexOf(parameter)
syntax: indexOf(param) or indexOf(param, position)
This method is used to find the index of the particular element in an array.
const arr = [ 10 , 20 , "chan" , "hi" , 40 ];
console.log(arr.indexOf("chan"));
concat
This method helps to concat 2 or more arrays into one.
The concat() method does not modify the existing arrays. It always returns a new array.
const arr1 = ["apple", "banana"];
const arr2 = ["red", "orange", "green"];
const arr3 = ["1", "2"];
const total_arr = arr1.concat(arr2, arr3);
console.log(total_arr );
map( )
This method creates a new array with the results of calling a provided function on every element in this array.
const array1 = [1, 4, 9, 16];
const mapExam = array1.map(x => x * 2);
console.log(mapExam);
filter( )
This method creates a new array with only elements that passes the condition inside the provided function.
const array1 = [1, 4, 9, 16];
const mapExam = array1.filter(x => x > 4);
console.log(mapExam);
Slice Method
This method is used to slice a piece of an array.
syntax
- array.slice(starting_index , ending_index+1)
const arr = [ "a" , "b" , "c" , "d" , "e" ];
let slt = arr.slice(1 , 4);
console.log(slt)
Splice Method
Whenever we want that add some new value at a particular index number as well as remove some value according to our choice, then in that case we used the splice method.
syntax
- array.splice(target_index , number_of_removing_elements , new_elements)
const arr = [ "Aa" , "Bb" , "Cc" , "Dd" , "Ee" ];
arr.splice(1 , 0, "Ff" , "Gg");
console.log(arr)
includes()
This method defines whether the array contains the specified element at the specified index number or not.
syntax
- array.includes(element, index-number);
const arr = [ "a" , "b" , "c" , "d" , "e" ];
console.log(arr.includes(d , 3); // true
console.log(arr.includes(d , 4); // false
Reverse anArray
This method is helps to reverse the existing array.
syntax
- array.reverse() ;
const arr = [ 10 , 20 , 30 , 40 , 50];
console.log(arr.reverse());
sort()
This method of the array is to rearrange our existing array in ascending order.
const arr = [ "chan" , "samp" , "Allf" , "anu" ];
let arr1 = arr.sort();
console.log(arr1);
split()
Converting a string into an array of elements separated by a comma.
The
split()
method does not modify the existing string. It always returns an array.
const str = "hello" ;
let arr = str.split("");
console.log(arr); //[ 'h', 'e', 'l', 'l', 'o' ]
console.log(str); // hello
join()
It will convert user array into a string seperated by the joining element.
let arr = [ "chan" , "samp" , "Allf" , "anu" ];
console.log(arr.join("/"));