Found 8895 Articles for Front End Technology

How can I remove the decimal part of JavaScript number?

Shubham Vora
Updated on 06-Sep-2023 10:51:38

43K+ Views

In this tutorial, we will learn to remove the decimal part of the number in JavaScript. While working with JavaScript, many times, developers need to play with the numbers stored in the database and needs to convert float and double numbers into the pure decimal or integer number.In this situation, developers can have multiple methods to truncate the decimal part of the number. We will see different approaches to overcome the above problem.Using the Math.trunc() methodUsing the Bitwise operatorsUsing the Math.ceil() and Math.floor() methodUsing the Math.Trunc() methodThe Math.trunc() is the most popular method to remove the decimal part of JavaScript. ... Read More

What is the best way to convert a number to a string in JavaScript?

vanithasree
Updated on 17-Jun-2020 06:37:14

139 Views

The best way to convert a number to a string is using the toString() method, with different bases. The method has an optional parameter “radix”, which is used as a base (2, 8, 16) for a numeric value.ExampleYou can try to run the following code to convert a number to a string −Live Demo                    var num = 10;          var val1 = num.toString();          var val2 = num.toString(2);          var val3 = num.toString(8);          var val4 = num.toString(16);          document.write(val1 + "" + val2 + "" + val3 + "" + val4);           Output10 1010 12 a

How to pad a number with leading zeros in JavaScript?

Shubham Vora
Updated on 06-Sep-2023 10:20:39

56K+ Views

In this tutorial, we will learn how to pad a number with leading zeros in JavaScript. Padding a number can be done in different ways in JavaScript. In this tutorial, we will see the two most popular ways of doing it − Using the String padStart() method Using the Array object constructor and join() method Using the String padStart() Method In JavaScript, the String object’s padStart() method is useful to pad a number with leading zeros. It takes two parameters, the first one is the total length of the targeted or desired string, and the other one is ... Read More

How to get a decimal portion of a number with JavaScript?

mkotla
Updated on 17-Jun-2020 06:35:57

3K+ Views

With JavaScript, use the % operator to get the decimal portion of a number.ExampleYou can try to run the following to get decimal portion −Live Demo                    var num1 = 5.3;          var num2 = 4.2;          var num3 = 8.6;          document.write(num1 % 1);          document.write(""+num2 % 1);          document.write(""+num3 % 1);           Output0.2999999999999998 0.20000000000000018 0.5999999999999996

How to check whether a checkbox is checked in JavaScript?

Shubham Vora
Updated on 06-Sep-2023 14:13:45

48K+ Views

In this tutorial, we will learn to check whether a checkbox is checked in JavaScript. The checkbox is the input type in the HTML, which works as the selection box. The radio buttons which belong to the same group allow users to select only one value. Still, the checkbox which belongs to the same group allows users to select multiple values. Also, you have many uses of checkboxes in your mind yourself. The HTML can add a checkbox to the webpage, but to add the behaviour to the checkbox, we must use JavaScript. Programmers can add different behaviours to the ... Read More

How to format hexadecimal Number in JavaScript?

Giri Raju
Updated on 17-Jun-2020 06:35:28

991 Views

Firstly convert your number to hexadecimal with leading zeros and then format it by adding dashes.ExampleYou can try to run the following code to format hexadecimal number −Live Demo                    var num = 32122;          var myHex = ("000000000000000" + num.toString(16)).substr(-16);          var resHex = myHex.substr(0, 8)+'-'+myHex.substr(8,4)+'-'+myHex.substr(12,4);          document.write(resHex);           Output00000000-0000-7d7a

How to format a rounded number to N decimals using JavaScript?

Anvi Jain
Updated on 17-Jun-2020 06:45:36

345 Views

Use the toFixed() method to format a rounded number to N decimals. The toFixed() method formats a number with a specific number of digits to the right of the decimal.ExampleYou can try to run the following code to form a rounded number to N decimals −Live Demo           JavaScript toFixed() Method                        var num = 15;          document.write("num.toFixed() is : " + num.toFixed());          document.write("");          document.write("num.toFixed() is : " + num.toFixed(2));          document.write("");          document.write("num.toFixed() is : " + num.toFixed(1));          document.write("");           Outputnum.toFixed() is : 15 num.toFixed() is : 15.00 num.toFixed() is : 15.0

How to remove leading zeros from a number in JavaScript?

Prabhdeep Singh
Updated on 07-Nov-2022 05:56:16

19K+ Views

In this tutorial, we will explore how we can remove leading zeroes from any number in JavaScript. JavaScript removes leading zeroes from an integer variable automatically, but this is not the case when we consider strings in JavaScript. It is possible that a string in JavaScript which represents a numerical value may contain leading zeroes. These leading zeroes may cause problems when we try to perform any operation on the numerical value the string represents. This is why we will explore the ways by which we can remove the leading zeroes from a number, expressed as a string in JavaScript. ... Read More

How to create a website without using HTML?

Fendadis John
Updated on 25-Sep-2019 07:52:30

3K+ Views

If you are having no knowledge of HTML or CSS, and want to create a website, then do not worry, you can easily create a website without writing even a single line of HTML code.Here are some ways to build a website without writing any HTML or line of code:Website BuildersWhen you will buy a website hosting plan, then the hosting company will provide you with free website builder option to easily create a website without even writing a single piece of HTML CodeContent Management SystemsUse Content Management System such as WordPress, Drupal or Joomla to develop a website, without ... Read More

How to create a modal popup using JavaScript and CSS?

Nishtha Thakur
Updated on 16-Jun-2020 13:05:24

467 Views

Creating a modal popup means adding a dialog box, which generates on click of a button and close when user clicks anywhere outside of the popup.Here’s how a popup looks like below with header and a text. You can also add a footer to it −To create a modal popup using CSS and JavaScript, try to run the following code −ExampleLive Demo                    .popup {             display: none;             position: fixed;             z-index: 1;   ... Read More

Advertisements