OneBite.Dev - Coding blog in a bite size

convert variable from float to int in Javascript

Code snippet on how to convert variable from float to int in Javascript

  let number = 5.8;
  let numberInt = parseInt(number);

This code snippet is for converting a float type variable to an integer type in JavaScript. The first line declares a variable ‘number’ and assigns it a value of 5.8, which is a float. The second line declares another variable ‘numberInt’ and assigns it the value of the ‘number’ variable after being converted to an integer. The parseInt() method is whatever is given to it, number in this case, and rounds it down to the nearest integer. For example, 5.8 is converted to 5. Therefore, numberInt is now assigned the value 5.

javascript