Open In App

How to convert a pixel value to a number value using JavaScript ?

Last Updated : 25 Jul, 2023
Comments
Improve
Suggest changes
1 Like
Like
Report

In this article, we will see how to convert the string value containing 'px' to the Integer Value with the help of JavaScript.

There are two approaches to converting a pixel value to a number value, these are:

  • Using parseInt() Method
  • Using RegExp

Approach 1: Using parseInt() Method

This method takes a string as the first argument and returns the Integer value.

Example: This example implements the above approach. 

Output:

fontSizeinNum

Approach 2: Using RegExp

The RegExp is used to replace the 'px' with an empty string and then convert the result to Integer using Number() method.

Example: This example implements the above approach.

Output:

fontSizeinNum


Next Article

Similar Reads