
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Largest and smallest word in a string - JavaScript
We are required to write a JavaScript function that takes in string and returns an array with two string values, and they should be the smallest and largest words respectively from the string.
For example −
If the string is −
const str = "Hardships often prepare ordinary people for an extraordinary destiny";
Then the output should be −
const output = ["an", "extraordinary"];
So, let's write the code for this function
Example
Following is the code −
const str = "Hardships often prepare ordinary people for an extraordinary destiny"; const largestSmallest = str => { const strArr = str.split(" "); let min = strArr[0]; let max = strArr[0]; for(let i = 1; i < strArr.length; i++){ if(strArr[i].length < min.length){ min = strArr[i]; }; if(strArr[i].length > max.length){ max = strArr[i]; }; }; return [min, max]; }; console.log(largestSmallest(str));
Output
The output in the console: −
[ 'an', 'extraordinary' ]
- Related Articles
- Program to find Smallest and Largest Word in a String in C++
- Finding second smallest word in a string - JavaScript
- Function to find the length of the second smallest word in a string in JavaScript
- Python program to find Largest, Smallest, Second Largest, and Second Smallest in a List?
- C# program to find Largest, Smallest, Second Largest, Second Smallest in a List
- Program to find the largest and smallest ASCII valued characters in a string in C++
- Finding shortest word in a string in JavaScript
- Twice repetitive word count in a string - JavaScript
- Finding the longest word in a string in JavaScript
- Java program to find Largest, Smallest, Second Largest, Second Smallest in an array
- Rearrange An Array In Order – Smallest, Largest, 2nd Smallest, 2nd Largest,. Using C++
- Finding the largest and smallest number in an unsorted array of integers in JavaScript
- Find word character in a string with JavaScript RegExp?
- Find non-word character in a string with JavaScript RegExp
- Find the Smallest element from a string array in JavaScript

Advertisements