
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Removing all spaces from a string using JavaScript
Problem
We are required to write a JavaScript function that takes in a string and return a new space free string (a string in which all spaces are replaced by empty strings).
Example
Following is the code −
const str = 'some random string ex a m pl e'; const removeSpaces = (str = '') => { let res = ''; for(let i = 0; i < str.length; i++){ const el = str[i]; if(el !== ' '){ res += el; continue; }; }; return res; }; console.log(removeSpaces(str));
Output
somerandomstringexample
- Related Questions & Answers
- Removing punctuations from a string using JavaScript
- Removing all non-alphabetic characters from a string in JavaScript
- Breaking a string into chunks of defined length and removing spaces using JavaScript
- Removing adjacent duplicates from a string in JavaScript
- Java Program to remove all white spaces from a String.
- Removing a specific substring from a string in JavaScript
- Find number of spaces in a string using JavaScript
- Java program to remove all the white spaces from a given string
- Removing first k characters from string in JavaScript
- Removing comments from array of string in JavaScript
- Removing Elements from a Double Linked List using Javascript
- Removing all the empty indices from array in JavaScript
- Removing n characters from a string in alphabetical order in JavaScript
- Summing numbers present in a string separated by spaces using JavaScript
- C++ Program to remove spaces from a string?
Advertisements