
- 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
Function to find the length of the second smallest word in a string in JavaScript
We are required to write a JavaScript function that takes in a string sentence as first and the only argument. And the function should return the length of the second smallest word from the string.
For example: If the string is −
const str = 'This is a sample string';
Then the output should be 2.
Therefore, let’s write the code for this function −
Example
The code for this will be −
const str = 'This is a sample string'; const secondSmallest = str => { const strArr = str.split(' '); if(strArr.length < 2){ return false; } for(let i = 0; i < strArr.length; i++){ strArr[i] = strArr[i].length; }; strArr.sort((a, b) => a - b); return strArr[1]; }; console.log(secondSmallest(str));
Output
The output in the console will be −
2
- Related Articles
- Finding second smallest word in a string - JavaScript
- Finding the length of second last word in a sentence in JavaScript
- Largest and smallest word in a string - JavaScript
- Python - Find the length of the last word in a string
- PHP program to find the length of the last word in the string
- Program to find Smallest and Largest Word in a String in C++
- C# program to find Largest, Smallest, Second Largest, Second Smallest in a List
- Python program to find the smallest word in a sentence
- Program to find kth smallest n length lexicographically smallest string in python
- Find the Smallest element from a string array in JavaScript
- Find the first maximum length even word from a string in C++
- Python program to find Largest, Smallest, Second Largest, and Second Smallest in a List?
- Java program to find Largest, Smallest, Second Largest, Second Smallest in an array
- Find the smallest and second smallest elements in an array in C++
- Find the second most repeated word in a sequence in Java

Advertisements