Posts

Showing posts from February, 2023

Using .split() method to convert string to array

Image
1) Using .split() method example one // Convert String to Array const fruits = 'Banana, Apple, Orange, Guava.' console. log ( 'This is a String :' , fruits); // Create an Array variable and use the .split() method const array = fruits. split ( ',' ); console. log ( 'This is an Array :' , array); ========================================================================================== TERMINAL: 2) Example 2 using \n // Convert String to Array const fruits = 'Banana \n Apple \n Orange \n Guava.' console. log ( 'This is a String :' , fruits); // Create an Array variable and use the .split() method const array = fruits. split ( ' \n ' ); console. log ( 'This is an Array :' , array); ========================================================================================== TERMINAL:

Using .join() method

Image
  1) Using .join() method example one // Convert to Mango * Orange * Banana * Grapes * Pineapple // 0 1 2 3 4 const fruits = [ 'Mango' , 'Orange' , 'Banana' , 'Grapes' , 'Pineapple' ]; console. log ( 'This is an Array:' , fruits); // Create a String variable and use the .join(' * ') method const string = fruits. join ( ' * ' ); // instead of * we can use anything console. log ( 'This is a string:' , string); // default seperator of string is comma (,) =============================================================================================== TERMINAL: 2) example 2 using \n // using \n // 0 1 2 3 4 const fruits = [ 'Mango' , 'Orange' , 'Banana' , 'Grapes' , 'Pineapple' ]; console. log ( 'This is an Array:' , fruits); // Create a String variable and use the ...

Converting Array to a String[ .tostring() ] method

Image
  1. Using the .tostring() method // Converting Array to a String // 0 1 2 3 4 const fruits = [ 'Mango' , 'Orange' , 'Banana' , 'Grapes' , 'Pineapple' ]; console. log ( 'This is an Array:' , fruits); // Convert this Array to a String by .tostring(); method // Create a String variable const string = fruits. toString (); console. log ( 'This is a string:' , string); // default seperator of string is comma (,) =============================================================================================== TERMINAL:

Adding elements to array ( push() & arr[arr.length] )

Image
  1) Two methods to add elements in an array // Adding array elements // 0 1 2 3 4 const fruits = [ 'Mango' , 'Orange' , 'Banana' , 'Grapes' , 'Pineapple' ]; // 1st by push method fruits. push ( 'Lemon' ); // push will add/push the elements at the end of the array console. log (fruits); // 2nd by arr.length method fruits[fruits.length] = 'Lemon' ; // fruits[5] console. log (fruits); ========================================================================================== TERMINAL:

looping in an array

Image
  1) looping in array // looping in an array // 0 1 2 3 4 const fruits = [ 'Mango' , 'Orange' , 'Banana' , 'Grapes' , 'Pineapple' ]; let i = 0 // initialization of while loop while (i < fruits.length) { console. log (fruits[i]); i ++ // increment of while loop } =============================================================================================== TERMINAL:

Finding last element in an array ( arr[arr.length - 1] )

Image
  1) Two methods to find last element of an array // last element in an array // 0 1 2 3 4 5 const arr = [ 'A' , 'B' , 'C' , true , 20 , 'xyz' ]; // 1st method console. log (arr[ 5 ]); // A // 2nd mehod console. log (arr[arr.length - 1 ]); // arr[6-1] => arr[5] =============================================================================================== TERMINAL:

Finding 1st element of an array ( arr[0])

Image
1) How to first element in an array // first element in an array // 0 1 2 3 4 5 const arr = [ 'A' , 'B' , 'C' , true , 20 , 'xyz' ]; console. log (arr[ 0 ]); // A =============================================================================================== TERMINAL:

arr.length method

Image
1) How to find length of an array    // array.length // 0 1 2 3 4 5 const arr = [ 'A' , 'B' , 'C' , true , 20 , 'xyz' ]; console. log (arr); // length of Array = last index + 1 // = 5+1 => 6 console. log (arr.length); // 6 =============================================================================================== TERMINAL:

Create Array in Javascript including new keyword

  1. Most used method // Creating an Array in Javascript const car = [ 'BMW' , 'Audi' , 'Mercedes' ]; console. log (car); =============================================================================================== TERMINAL: Nobins-MacBook-Pro:Array_Module 24SEPT 2022 nobinpunyo$ node array.js [ 'BMW' , 'Audi', 'Mercedes' ] 2. Initialising with empty Array method //2nd Method Initializing with empty array const fruit = []; // Initializing with empty array fruit[ 0 ] = 'Mango' ; fruit[ 1 ] = 'Orange' ; fruit[ 2 ] = 'Apple' ; fruit[ 3 ] = 'Grapes' ; console. log (fruit); =============================================================================================== TEMINAL: Nobins-MacBook-Pro:Array_Module 24SEPT 2022 nobinpunyo$ node array.js [ 'Mango', 'Orange', 'Apple', 'Grapes' ] 3. Using new Keyword method // 3rd Mehod , using new keyword const books = new Arr...