
- 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 leading zeros in array in JavaScript
The requirements for this question are simple. We are required to write a JavaScript function that takes in an array of Numbers.
If the array contains leading zero, the function should remove the leading zeros in place, otherwise the function should do nothing.
For example: If the input array is −
const arr = [0, 0, 0, 14, 0, 63, 0];
Then the output should be −
const output = [14, 0, 63, 0];
Therefore, let’s write the code for this function −
Example
The code for this will be −
const arr = [0, 0, 0, 14, 0, 63, 0]; const removeLeadingZero = arr => { while (arr.indexOf(0) === 0) { arr.shift(); }; }; removeLeadingZero(arr); console.log(arr);
Output
The output in the console will be −
[ 14, 0, 63, 0 ]
- Related Articles
- Remove leading zeros in array - JavaScript
- Remove leading zeros in a JavaScript array?
- Remove Leading Zeros from an Array using C++
- How to remove leading zeros from a number in JavaScript?
- Remove Leading Zeros From String in Java
- Remove Leading Zeros from a String in C#
- Java Program to Remove leading zeros
- Golang program to remove leading zeros
- Python program to remove leading zeros from an IP address
- How to pad a number with leading zeros in JavaScript?
- Remove leading zeros from a Number given as a string using Python
- JavaScript Regex to remove leading zeroes from numbers?
- Add leading Zeros to Python string
- Print leading zeros with C++ output operator
- Add leading zeros to a MySQL column?

Advertisements