Lodash String_.startsWith and _.endsWith Example

The _.startsWith() function determines if the string starts with the specified string. The _.endsWith() function determines if the string ends with the specified string.

Learn Lodash JS at Lodash JS Tutorial with Examples.

Lodash String_.startsWith and _.endsWith Example

<!DOCTYPE html>
<html>
<head>
    <title>Lodash Tutorial</title>
    <script src="https://cdn.jsdelivr.net/npm/lodash@4.17.11/lodash.min.js"></script>

    <script type="text/javascript">
  const words = ["tank", "boy", "tourist", "ten",
    "pen", "car", "marble", "sonnet", "pleasant",
    "ink", "atom"]

  console.log("Starting with 't'");
  words.forEach( e => {

   if (_.startsWith(e, 't')) {

    console.log(e);
   }
  });

  console.log("Ending with 'k'");
  words.forEach( e => {

   if (_.endsWith(e, 'k')) {

    console.log(e);
   }
  });
    </script>
</head>
<body></body>
</html>
Output:
Starting with 't'
tank
tourist
ten
Ending with 'k'
tank
ink

Reference


Comments