Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

get biggest element in array javascript

// first of all, i write a forEach loop function

// raw fanction
function getArrayLargestElement(array) {
  let largestElement = "";
  array.forEach(largest);
  function largest(word) {
    if (word.length > largestElement.length) {
      largestElement = word;
    }
  }
  return largestElement;
}

const banglaGit = [
  "amader",
  "sonar",
  "bangladesh",
  "ami tomay",
  "onek ",
  "valo basi",
];

console.log(getArrayLargestElement(banglaGit));

// es6 arrow function

const getLargestArrowFunc = (array) => {
  let largest = "";
  array.forEach((word) => {
    if (word.length > largest.length) {
      largest = word;
    }
  });
  return largest;
};

console.log(
  getLargestArrowFunc(["hello", "world", "md yeamin", "i'm", "here"])
);
Source by 1loc.dev #
 
PREVIOUS NEXT
Tagged: #biggest #element #array #javascript
ADD COMMENT
Topic
Name
8+1 =