
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 Zeros from Start and End in JavaScript
We are required to write a JavaScript function that takes in a number as a string and returns a new number string with all the leading and trailing 0s removed
For example: If the input is −
const strNum = '054954000'
Then the output should be −
const output = '54954'
Example
Following is the code −
const strNum = '054954000'; const removeZero = (str = '') => { const res = ''; let startLen = 0, endLen = str.length-1; while(str[startLen] === '0'){ startLen++; }; while(str[endLen] === '0'){ endLen--; }; return str.substring(startLen, endLen+1); }; console.log(removeZero(strNum));
Output
Following is the output in the console −
54954
Advertisements