- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Shedding a string off whitespaces 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
The code for this will be −
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 −
Thisisanexamplestringfromwhichallwhitespaceswillberemoved
- Related Articles
- Remove all whitespaces from string - JavaScript
- Remove the whitespaces from a string using replace() in JavaScript?
- C# Program to remove whitespaces in a string
- Delete all whitespaces from a String in Java
- How to replace duplicate whitespaces in a String in Kotlin?
- Java Program to Remove All Whitespaces from a String
- Arranging lexicographically and removing whitespaces in JavaScript
- What is load shedding in computer networks?
- Whitespaces in Perl
- Take off last character if a specific one exists in a string?
- Switching on and off bulb in JavaScript
- Update all rows in MySQL and remove all the unnecessary whitespaces in and around the string?
- Trim off '?' from strings in JavaScript
- Removing whitespaces using C# Regex
- Removing whitespaces using C# String.Empty

Advertisements