
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Remove All Whitespaces from String in JavaScript
We are required to write a JavaScript function that takes in a string and returns a new string with all the character of the original string just the whitespaces removed.
Example
Let’s write the code for this function −
const str = "This is an example string from which all whitespaces will be removed"; const removeWhitespaces = str => { let newStr = ''; for(let i = 0; i < str.length; i++){ if(str[i] !== " "){ newStr += str[i]; }else{ newStr += ''; }; }; return newStr; }; console.log(removeWhitespaces(str));
Output
The output in the console after removing whitespaces −
Thisisanexamplestringfromwhichallwhitespaceswillberemoved
Advertisements