
- 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
Remove all whitespaces from string - 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
- Related Articles
- Java Program to Remove All Whitespaces from a String
- Golang program to remove all whitespaces from a string
- Remove the whitespaces from a string using replace() in JavaScript?
- Delete all whitespaces from a String in Java
- Update all rows in MySQL and remove all the unnecessary whitespaces in and around the string?
- Remove all characters of first string from second JavaScript
- C# Program to remove whitespaces in a string
- Shedding a string off whitespaces in JavaScript
- How can we use MySQL TRIM() to remove the whitespaces from all the rows and update table?
- Write a Regular Expression to remove all special characters from a JavaScript String?
- JavaScript Remove non-duplicate characters from string
- JavaScript - Remove first n characters from string
- How to remove all whitespace from String in Java?
- Remove all duplicates from a given string in C#
- Remove all duplicates from a given string in Python

Advertisements