
- 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
Removing 0s from start and end - 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
- Related Articles
- Swap certain element from end and start of array - JavaScript
- Removing an element from the start of the array in javascript
- Removing an element from the end of the array in Javascript
- JavaScript Program to Find Maximum number of 0s placed consecutively at the start and end in any rotation of a Binary String
- Display all the numbers from a range of start and end value in JavaScript?
- How to calculate end date from start date and duration in Excel?
- Removing Negatives from Array in JavaScript
- Removing redundant elements from array altogether - JavaScript
- JavaScript Algorithm - Removing Negatives from the Array
- Removing duplicate objects from array in JavaScript
- Removing punctuations from a string using JavaScript
- Removing parentheses from mathematical expressions in JavaScript
- Removing an element from an Array in Javascript
- Removing first k characters from string in JavaScript
- Removing property from a JSON object in JavaScript

Advertisements