Posts

Delete the april month from an array using Splice Method and indexOf method

  <! DOCTYPE html > < html lang = "en" > < head >   < meta charset = "UTF-8" >   < meta name = "viewport" content = "width=device-width, initial-scale=1.0" >   < title >Splice method </ title > </ head > < body >   < h1 >Delete the april month from an array using Splice Method and indexOf method</ h1 >   < script >       const arrMonths = [ 'jan' , 'feb' , 'march' , 'april' , 'may' ];     // arrMonths.splice(3, 1);     console . log (arrMonths);     const deleteApril = "march" ;     const delIndex = arrMonths . indexOf (deleteApril);     console . log ( `Index of ${ deleteApril } = ${ delIndex }` );     if (delIndex != -1 ){       arrMonths . splice (delIndex, 1 );       console . log (arrMonths);     } else {       console . log ( "Elemen not fou...

JavaScript Splice() method

  Syntax array .splice( index ,  howmany ,  item1 , .....,  itemX ) Parameters Parameter Description index Required. The position to add/remove items. Negative value defines the position from the end of the array. howmany Optional. Number of items to be removed. item1 , ...,  itemX Optional. New elements(s) to be added. <! DOCTYPE html > < html lang = "en" > < head >   < meta charset = "UTF-8" >   < meta name = "viewport" content = "width=device-width, initial-scale=1.0" >   < title >Splice method </ title > </ head > < body >     < h1 >Javascript Splice Method</ h1 >   < script >       // Aditya = Aditya Sharma       // array.splice(index, howmany deletes, item1, ....., itemX)       const myArr = [ "Seema" , "Aditya" , "Reema" , "Vandana" ];       //  myArr.splice(1,0, "Aditya Sharma"); ...

JavaScript Slice() method

  JavaScript Array slice() array .slice( start ,  end ) Description The  slice()  method returns selected elements in an array, as a new array. The  slice()  method selects from a given  start , up to a (not inclusive) given  end . The  slice()  method does not change the original array.   const fruits = [ "Banana" , "Orange" , "Lemon" , "Apple" , "Mango" ];                 //  -5         -4      -3         -2          -1     console . log (fruits . slice ( -4 , -1 ));

Javascript Array Concat and Join Method

  The JavaScript   Array concat()   Method is used to merge two or more arrays together. This method does not alter the original arrays passed as arguments. but instead return new Array. let newArray1 = oldArray.concat() let newArray2 = oldArray.concat(value0) let newArray3 = oldArray.concat(value0,value1) ....... ....... let newArray = oldArray.concat(value1 , [ value2, [ ...,[ valueN]]]) -------------------------------- Description The  join()  method returns an array as a string. The  join()  method does not change the original array. Any separator can be specified. The default is comma (,). <! DOCTYPE html > < html lang = "en" > < head >   < meta charset = "UTF-8" >   < meta http-equiv = "X-UA-Compatible" content = "IE=edge" >   < meta name = "viewport" content = "width=device-width, initial-scale=1.0" >   < title >Document</ title > </ head > < body >   ...

JavaScript Array Shift() method

  JavaScript  Array Shift() method The   shift()   method removes   the first item   of an array. The  shift()  method changes the original array. The  shift()  method returns the shifted element. <! DOCTYPE html > < html lang = "en" > < head >   < meta charset = "UTF-8" >   < meta http-equiv = "X-UA-Compatible" content = "IE=edge" >   < meta name = "viewport" content = "width=device-width, initial-scale=1.0" >   < title >Document</ title > </ head > < body >   < h1 >JavaScript Array shift()</ h1 >   < script >     const fruits = [ "Banana" , "Orange" , "Apple" , "Mango" ];     const delElem = fruits . shift ();     console . log (delElem);     console . log (fruits);       </ script > </ body > </ html >

JavaScript pop() Method

  Definition and Usage The  pop()  method removes (pops)  the last element  of an array. The  pop()  method changes the original array. The  pop()  method returns the removed element. <! DOCTYPE html > < html lang = "en" > < head >   < meta charset = "UTF-8" >   < meta http-equiv = "X-UA-Compatible" content = "IE=edge" >   < meta name = "viewport" content = "width=device-width, initial-scale=1.0" >   < title >Document</ title > </ head > < body >   < h1 >JavaScript Array pop() Method</ h1 >   < script >     const myNumbers = [ 1 , 4 , 8 , 4 ];         console . log (myNumbers);     console . log (myNumbers . pop ());     console . log (myNumbers);           </ script > </ body > </ html >

JavaScript unshift() Method

  Definition and Usage The  unshift()  method adds new elements to  the beginning  of an array. The  unshift()  method overwrites the original array. Return Value Type Description A number The new length of the array. syntex array .unshift( item1 ,  item2 , ...,  itemX ) <! DOCTYPE html > < html lang = "en" > < head >   < meta charset = "UTF-8" >   < meta http-equiv = "X-UA-Compatible" content = "IE=edge" >   < meta name = "viewport" content = "width=device-width, initial-scale=1.0" >   < title >Document</ title > </ head > < body >   < h1 >JavaScript Array unshift() Method</ h1 >   < script >     const fruits = [1, 4, 8, 4];     console . log (fruits . unshift ( 2 ));     console . log (fruits);   </ script > </ body > </ html >