Warning: session_start(): open(/tmp/sess_cdafc70187c8aad9724c23989a22f983, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
How To Add Leading Zeros To A String In JavaScript? - LearnShareIT

How To Add Leading Zeros To A String In JavaScript?

Add Leading Zeros to a String in JavaScript

In JavaScript, we have many methods to work with a string. This article will help you learn how to add leading zeros to a string in JavaScript. We can also use one of these methods, like padStart(), or add the simple string with the ‘0’ character.

Add leading zeros to a string in JavaScript

Using the (+) operator

You can use the (+) operator to add leading zeros to a string in Javascript as following.

Example:

var nums = ["1","2","9","10"];
function addLeadingZeros(totalLength, str) {
   // Add leading zeros using the (+) operator
   while (str.length < totalLength) {
     	str = "0" + str;
   }
   console.log(str);
}

addLeadingZeros(3, nums[0]); // "001"
addLeadingZeros(3, nums[1]); // "002"
addLeadingZeros(3, nums[2]); // "009"
addLeadingZeros(3, nums[3]); // "010"

I built a function to add leading zeros with the (+) operator combined with a While loop. With this function, we can get a string with some characters equal to ‘totalLength’ and have one or more a string literal ‘0’ if the passed ‘str’ is less than ‘totalLength’. With string ‘str’ longer than ‘totalLength’, we will get a string unchanged from the original string.

Using the padStart() method

padStart() method helps our program add a character or a string of multiple characters to the beginning of the called string provided that the number of characters of the string used to call the method has ‘length’ less than the ‘length’ we passed when function call.

Syntax:

padStart(totalLength, padString)

or

padStart(totalLength)

Parameters:

  • totalLength: This is the length of the string to which we want the pad to add the value.
  • padString: This is the string you want to pad when the string used to call the method has fewer characters than ‘totalLength’.

Example:

var nums = ["1", "2", "9", "10"];

// Using padStart() method with one parameter
nums[0].padStart(3); // "  1"
nums[1].padStart(3); // "  2"
nums[2].padStart(3); // "  9"
nums[3].padStart(3); // " 10"

We see that when we pass only one parameter, our ‘padString’ is the ‘space’ character, so we want to add leading zeros for this article. We have to pass two parameters when we call the padStart method (), and I would do it like this:

var nums = ["1", "2", "9", "10"];

// Using padStart() method to add leading zeros
nums[0].padStart(3, "0"); // "001"
nums[1].padStart(3, "0"); // "002"
nums[2].padStart(3, "0"); // "009"
nums[3].padStart(3, "0"); // "010"

With the padStart() method, we can add leading zeros and any string or character we want by changing ‘padString’ when we call the method.

Summary

Through the above article, we already know how to add leading zeros to a string in JavaScript. Of the two ways using the (+) operator and using padStart() method, the most convenient way is using padStart() method because it will reduce costs and shorten the code of your program. Try using both ways for your program. Good luck!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *