How many users does Parse

Convert strings to numbers and check

Only number, no integer, no floating point

Unlike most programming languages, Javascript only knows one type of number - no integer, short, long or floating point numbers. Numbers are simply number, with or without decimal places.

parseInt, parseFloat, isFinite and encodeURI / decodeURI are independent of objects and are therefore referred to as "top-level functions". Otherwise, most of the Javascript functionality is provided by objects and their methods.

In any case, be careful when entering forms: Javascript requires a point as a decimal separator!

parseInt ()

Parses (parses) a string "x" and returns the first number (integer) it finds.

parseInt (x, [radix])

If the first character is not a digit, space, or a leading minus sign, there parseInt ()NaN back.

parseInt () supports a second optional parameter radixto specify the base of the parsed number (in the range 2-36).

  • With the value "10" the parser interprets the number in the decimal system, while "16" interprets a number as a hexadecimal value.
  • If the parameter does not exist, works parseInt () assume that numbers beginning with "0x" radix 16 are, "0" radix 8 and all other numbers radix 10.
parseInt ("50") // returns 50 parseInt ("3-time master") // returns 3 parseInt ("5 lives") // returns 5 parseInt ("I have 3 computers") // returns NaN parseInt ("17", 8) // returns 15

As a precaution, base 10 should always be specified when evaluating form fields so that leading zeros do not lead to errors.

parseInt ('017') // returns 15 parseInt ('17 ') // returns 17

parseFloat (x)

Parses a string x and returns the first floating point number - i.e. the first number with a decimal point. If the first character in the string is not a digit, white space or a leading minus sign, there is parseFloat ()NaN back.

parseFloat ("15.87") // returns 15.87 parseFloat ("-17.8 meters") // returns -17 parseFloat ("foo") // returns NaN

parseFloat () e.g. converts entries in form fields into numbers. Javascript always interprets the values ​​of form fields as a string, even if the user has entered '17 .87 'in a field. When transforming floating point numbers from form fields, commas must be used before calling parseInt () be converted into points since parseInt () otherwise only converts the number to the decimal point.

x = x.replace (/, /, "."); y.value = parseFloat (x);

isNaN ()

isNaN () checks whether x has the value NaN (Not a Number) - an illegal number.

isNaN (document.getElementById ('nannum'). value)

isNaN there true returns if x is not an ordinary number (NaN) and otherwise false. A typical example for NaN is »0 divided by 0«, but when checking entries in form fields, you must also check whether the entry can actually be converted into a number. IsNaN is required for the check, since the equality operators do not work for both an if (num == NaN) and if (num === Nan) query.

isFinite (x)

Determines whether a number is finite. Gives false back if x + is infinity, -infinity or NaN (from JavaScript 1.3).

alert (2.17424); // returns true alert (1.5E + 317); // returns false alert (17/3); // returns true alert (0/0); // returns false

eval (s)

Javascript eval first checks whether s is a string, then develops the string and executes it if the string contains Javascript instructions. At the end, eval () outputs the value of the last instruction - i.e. eval () executes instructions that are available as a character string - e.g. from a user input. eval () allows the dynamic construction of JavaScript statements and expressions.

eval () expects a string. If the string represents a valid expression, results eval () the expression. The argument is optional - if no argument is passed, eval () returns undefined. If the argument is not a string, returns eval () returns the argument unchanged.

function quad (num) {return (num * num); } let sinn = 42; eval ("42 + 8 * 2"); // returns 58 eval ("sinn - quad (3)"); // returns 33 eval ("let z = 0; for (let i = 0; i Of course it will eval () not called to execute an expression as in these examples. Javascript executes expressions automatically. Is useful eval () E.g. when expressions are taken from a form field as a string as user input.

eval () is considered questionable, unclean to nasty: In the input field of the example, not only pious mathematical expressions are developed and harmless Javascript instructions such as alert ('Hello world')but every instruction. Javascript eval (s) is a popular basis for obfuscating hacks and Trojans, e.g. for redirects to advertising popups from an external Javascript.

Number (obj)

Javascript Number (obj) converts the object in the argument to a number that represents the object. If the object cannot be converted into a number, there is Number () NaN (not a number). The object must be a Javascript object. If no argument is passed, gives Number () 0 back.

let foo = new Boolean ("true"); alert (Number (foo));

returns 1

String (obj)

Javascript String (obj) creates a string from any object.

The argument obj must be a Javascript object. If no argument is passed, gives String () returns an empty string.

let items = new Array ("Elvira", "Rosinante", 14, true); alert (String (items));

gives Elvira, Rosinante, 14, true out.