JavaScript built-in function: parseInt(), parseFloat(),String(),eval()

(Last Updated On: April 2, 2021)

JavaScript Built-in Functions:

JavaScript built-in function JavaScript provides several built-in functions that can be used to solve different kinds of problems. In his article, the most commonly used conversion functions are discussed. These functions are used to convert data from one type to another.

Amazon Purchase Links:

Top Gaming Computers

Best Laptops

Best Graphic Cards

Portable Hard Drives

Best Keyboards

Best High Quality PC Mic

Computer Accessories

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

parseInt() JavaScript built-in function:

this function is used to convert a string (string of digits such as “145”) to an integer. This JavaScript built-in function is take a string as an argument and returns an integer value. if the string does not begin with an integer, this function returns NaN( not a number).

The parseInt() JavaScript built-in function is also used to convert the floating-point number into integer. It takes a floating-point number as an argument and returns an integral part of the number. For example, parseInt(23.43) will return 23.

The general syntax of parseInt() JavaScript built-in function is,

parseInt(String);

where “string” represent the string of digits.

Examples:

  • parseInt(“12bx345”) returns 12.
  • parseInt(“abcd”)returns NaN( not a number)
  • parseInt(“7FAB67”) returns 7.
  • parseInt(“ABC793xyz”) returns NaN.
  • parseInt(12.5487) returns 12.


Write JavaScript code to demonstrate the use of parseInt JavaScript built-in function:

JavaScript built-in function

parseFloat() JavaScript built-in function:

this JavaScript built-in function is used to convert the string (string of digits along with decimal point) into floating point number. This function takes a string as an argument and returns a floating point number. It the string does not begin with an integer, this built-in function returns NaN(not a number).

The general syntax of parseFloat() JavaScript built-in function i is:

parseFloat(string);

Examples:

  • parseFloat(“12.627”) returns 12.627.
  • parseFloat (“abcd 12.34”)returns NaN( not a number)
  • parseFloat (“12FAB67”) returns 12.
  • parseFloat (“12.abc”) returns 12.
  • parseFloat (“12.0abc”) returns 12.

It may be noted that parseFloat() JavaScript built-in function is also first integer number in a string. Similarly, if the number has decimal point at its end or value 0 point then this built-in function also returns integer value.



Write JavaScript code to demonstrate the use of parseFloat JavaScript built-in function:

JavaScript built-in function

toFixed() JavaScript built-in function:

this JavaScript built-in function is used to fix (or limit) the number of digits displayed after a decimal point. The output value is rounded off. For example, to display a value 49.917 with two digits of precision, the value 49.92 will be displayed on the output.

The general syntax of toFixed JavaScript built-in function is;

Value.toFixed(n);

Where

Value specifies the value or variable.

n specifies the number of digits to be fixed after the decimal point.


Write a program to print a floating-point number using toFixed() JavaScript built-in function:

JavaScript built-in function

String() JavaScript built-in function:

This JavaScript built-in function is used to convert a numeric value into a string.

The general syntax of Stirng() JavaScript built-in function i is;

String(value);

Where ‘value’ may be integer or floating point value

Example:

  • String(420) returns string of digits “420”
  • String (63.323) returns string of digits “63.323”

eval() JavaScript built-in function:

this JavaScript built-in function can be used to convert a string expression to a numeric value. it takes a string expression(such as “10*5”) as parameter, evaluate the expression and returns the result as numeric value.

the general syntax of eval() JavaScript built-in function is;

eval(“string expression”);

example:

  • Eval(“4.12 * 100”) returns 412.
  • Eval(“4.12 * 10”) returns 41.2


Write JavaScript code to demonstrate the use of eval() JavaScript built-in function:

JavaScript built-in function

Recommended For You

About the Author: Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Leave a Reply