Important JavaScript Functions You Need to Know About.

Published: 2021-05-07 14:54:37

How to use JavaScript fundamental:

1.slice():   extracts a section of a string and returns it as a new string. If you call slice() on a string without passing it any additional information, it will return the whole string.
Example:  let  friends = [“Tania”, “Mim”, “Tasnim”, “Jannat”, “Nilufa”] ;
                   Console.log( friends . slice(2 , 4) ) ;
                 Run:  [ Tasnim,  jannat ]

2.present:  The parseInt is a function of converts its argument to a string and return an integer or NAN. A radix parameter is used to define the numeral system that used. These are represented by a number from 2 to 36. parseInt( ) JavaScript function can have two parameters, the string you want to convert and the radix which identifies the numeral system.
Example:  using parseInt()
1. Console.log (parseInt (“985.99”, 20));  //985
2. Console.log (parseInt (“F”, 16));  //15
3. Console.log (parseInt (“Hello”, 10));   // NaN

3. parseFloat ():  The parseFloat function is used to accept the string and convert it into a floating-point number. If the string does not contain a numeral value or if the first character of the string is not a number then it returns Nan. It actually returns a floating-point number parsed up to that point where it encounters a character that is not a Number. 
Example: Using parseFloat()
Console.log (parseFloat(“ 30”)):  //30
Console.log (parseFlat(“ 3.15”));  // 3.15
Console.log (parseFlat(“ JavaScript”));  // NaN
Console.log (parseFlat(“2021Javascript”));  // 2021

4.Array reduce (): The JavaScript Array reduce() method execute a reducer function on each element of the array and returns a single output value. Reduce () executes the given function for each value from left to right. Reduce () does not change the original array.
Example:
Const numbers = [ 12, 2, 30, 4, 15];
Const sumReducer = (accumulator, currentValue)  => accumulator + currentValue;
Console.log( numbers . reduce (sumReducer ) );    // 63

5. Array filter (): The JavaScript Array filter () method returns a new array with all elements that pass defined by the given function. The JavaScript filter () does not affect the initial array. It applies the callback function to every element to create a new collection with filtered elements. The callback function is with three arguments:
1. The value of the element
2. The index of the element
3. The Array object being traversed 
Example:
Const students = [
{Id: 1, name: “Lota”, roll: 101},
{Id: 2, name: “Jomuna”, roll: 102},
{Id: 3, name: “Moriom”, roll: 103}
] 
Const filterStudent =students.filter(student => student. roll<102);
Console.log (filterStudent);
Output: [ {id:1 , name: “Lota”, roll: 101} ]

6. Array find():  The JavaScript Array find() method returns the value of the first array element that satisfies the provided test function.
Example: 
Function isEven(element){
Returns element % 2 == 0;
}
Let randomArray = [1, 45, 8, 98,  7 ];
firstEvent = randomArray. Find( isEvent);
console.log ( firstEvent );  // 8

7. Array map():  The JavaScript Array map() method create a new array with the result of calling a function for every array element. The JavaScript map function is used to invoke a function for every element of an array in order. It should be noted that it does not execute the function on the element without values. The JavaScript map method does not change the original array.
Example:
Const array = [16, 12, 15, 10]
Const result = array. Map (  num => num * 2);
Console.log (result);
Output: Array [32, 24, 30, 20 ]

 8.  Array pop (): The JavaScript Array pop () method removes the last element from an array and returns that element. Removes the last elements from the array and returns that value. Return undefined if the array is empty. This method changes the original array and its length. 
Example:
Let languages: [  “React” , “JavaScript” , “Java” , “C”, “C++” ]
Let popped = languages. pop () ;
Console.log (languages) ;  //    [ “React” , “JavaScript” , “Java” , “C” ]
Console.log (popped) ;  // C++

9. Array push ():  The JavaScript Array push () method adds zero or more elements to the end of an array and returns the new length of the array. Retunes the new length of the array upon which the method was called.  This method changes the original array and its length.
Example:
Const fruits = [ ‘Apple’ , ‘Orange’ , ‘Lemon’ ]
Const  result  = fruits  . push (‘Avocado , ‘Banana , ‘Blackberries )
Console.log (fruits)  //  = [ ‘Apple’ , ‘Orange’ , ‘Lemon’ , (‘Avocado’ , ‘Banana , ‘Blackberries’]
Console.log (result)  //  6

10. Array indexOf:  The JavaScript array indexOf() method returns the first index of occurrence of an array element. Returns the first index of the element in the array if it is present at least once.  Return -1 if the element is not found in the array.
Example:
Const priceList = [ 20, 8, 23, 15, 26, 3 ,10 ] ;
Const result = pricelist. indexOf (26);
Console .log (result);  // 4