String Function (Number)

Course- Javascript >

We use the term data type to talk about the nature of the data that a variable contains. A string variable contains a string, a number variable, a numerical value, and so forth. However, the JavaScript language is what’s called a loosely typed language, meaning that JavaScript variables can be interpreted as different data types in differing circumstances.

In JavaScript, you don’t have to declare the data type of a variable before using it, as the JavaScript interpreter will make its best guess. If you put a string into your variable and later want to interpret that value as a number, that’s OK with JavaScript, provided that the variable actually contains a string that’s “like” a numerical value (for example, “200px” or “50 cents”, but not something such as your name). Later you can use it as a string again, if you want.

Mathematicians have all sorts of names for different types of numbers. From the socallednatural numbers 1, 2, 3, 4 ..., you can add 0 to get the whole numbers 0, 1, 2, 3, 4 ..., and then include the negative values -1, -2, -3, -4 ... to form the set of integers.

To express numbers falling between the integers, we commonly use a decimal point with one or more digits following it: 3.141592654

Calling such numbers floating point indicates that they can have an arbitrary number of digits before and after the decimal point; that is, the decimal point can “float” to any location in the number.

JavaScript supports both integer and floating-point numbers.

Integers

An integer is a whole number—positive, negative, or zero. To put it another way, an integer is any numerical value without a fractional part.

All of the following are valid integers:

  • 33
  • -1,000,000
  • 0
  • -1

Floating-Point Numbers

Unlike integers, floating-point numbers have a fractional part, even if that fractional part is zero. They can be represented in the traditional way, like 3.14159, or in exponential notation, like 35.4e5.


In exponential notation, e represents “times 10 to the power,” so 35.4e5 can be read as 35.4 x 105.

Exponential notation provides a compact way to express numbers from the very large to the very small.

All the following are valid floating-point numbers:

  • 3.0
  • 0.00001
  • - 99.99
  • 2.5e12
  • le-12

Not a Number (NaN)

NaN is the value returned when your script tries to treat something non-numerical as a number, but can’t make any sense of it as a numerical value. For example, the result of trying to multiply a string by an integer is not numerical. You can test for non-numerical values with the isNaN() function:

isNaN(3); // returns false
isNaN(3.14159); // returns false
isNaN("horse"); // returns true;                     
                     

Using parseFloat() and parseInt()

JavaScript offers us two functions with which we can force the conversion of a string into a number format.

The parseFloat() function parses a string and returns a floating-point number. If the first character in the specified string is a number, it parses the string until it reaches the end of that number, and returns the value as a number, not a string:

parseFloat("21.4") // returns 21.4
parseFloat("76 trombones") // returns 76
parseFloat("The magnificent 7") // returns NaN                     
                     

Using parseInt() is similar, but returns either an integer value or NaN. This function allows us to optionally include, as a second argument, the base (radix) of the number system we’re using, and can therefore be used to return the base 10 values of binary, octal, or other number formats:

parseInt(18.95, 10); // returns 18
parseInt("12px", 10); // returns 12
parseInt("1110", 2); // returns 14
parseInt("Hello") // returns NaN                     
                     

Infinity

Infinity is a value larger than the largest number that JavaScript can represent. In most JavaScript implementations, this is an integer of plus or minus 253. OK, that’s not quite infinity, but it is pretty big.

There is also the keyword literal -Infinity to signify the negative infinity.

You can test for infinite values with the isFinite() function. The isFinite() function takes the value to test as an argument and tries to convert that argument into a number. If the result is NaN, positive infinity (Infinity), or negative infinity (- Infinity), the isFinite() function returns false; otherwise it returns true. (False and true are known as Boolean values, discussed later in this section.)

isFinite(21); // true
isFinite("This is not a numeric value"); // false
isFinite(Math.sqrt(-1)); // false