JavaScript

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

JavaScript built-in function

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.

parseInt() 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() 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() 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 built-in function:

JavaScript built-in function

parseFloat() 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() 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() built-in function is also the first integer number in a string. Similarly, if the number has decimal point at its end or value 0 points then this built-in function also returns an integer value.


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

JavaScript built-in function

toFixed() 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 built-in function is;

Value.toFixed(n);

Where

The 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() built-in function:

JavaScript built-in function

String() built-in function:

This JavaScript built-in function is used to convert a numeric value into a string. The general syntax of String() built-in function i is;

String(value);

Where ‘value’ may be an integer or floating-point value

Example:

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

eval() 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 a parameter, evaluates the expression, and returns the result as a numeric value.

the general syntax of eval() 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() built-in function:

JavaScript built-in function

Related Article:

https://programmingdigest.com/event-handling-in-javascript-image-form-link-buttons-fileupload/

Related Articles

Leave a Reply

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

Back to top button