Thursday, 30 August 2018

JavaScript: isFinite: Check whether number is finite (or) not

Using isFinite function, you can check whether a number is finite (or) not. It treats all the finite numbers and Boolean values as finite numbers.

finite.html
<!DOCTYPE html>

<html>

<head>
    <title>Check Whether number is finite (or) not</title>
</head>

<body>
    <script>
        var a = 10;
        var b = Infinity;
        var c = -Infinity;
        var d = NaN;
        var e = "Hello";
        var f = true;
        var g = false;

        document.write("a = " + a + ", isFinite = " + isFinite(a) + "<br />");
        document.write("b = " + b + ", isFinite = " + isFinite(b) + "<br />");
        document.write("c = " + c + ", isFinite = " + isFinite(c) + "<br />");
        document.write("d = " + d + ", isFinite = " + isFinite(d) + "<br />");
        document.write("e = " + e + ", isFinite = " + isFinite(e) + "<br />");
        document.write("f = " + f + ", isFinite = " + isFinite(f) + "<br />");
        document.write("g = " + g + ", isFinite = " + isFinite(g) + "<br />");
    </script>
</body>

</html>


Open above page in browser, you can able to see below output.
a = 10, isFinite = true
b = Infinity, isFinite = false
c = -Infinity, isFinite = false
d = NaN, isFinite = false
e = Hello, isFinite = false
f = true, isFinite = true
g = false, isFinite = true


Previous                                                 Next                                                 Home

Wednesday, 29 August 2018

JavaScript: Rounding float values

‘floor’, ‘ceil’, ‘round’ functions are used to perform rounding on float values.

Function
Description
Math.floor(number)
Get the floor value of a number.
Math.ceil(number)
Get the ceil value of a number.
Math.round(number)
Round the number to nearest integer.

var a = Math.floor(5.9);         // Return 5
var b = Math.ceil(5.1);          // Return 6
var c = Math.round(5.4);       // Return 5
var d = Math.round(5.5);       // Return 6

round.html
<!DOCTYPE html>

<html>

<head>
    <title>rounding example</title>
</head>

<body>
    <script type="text/javascript">
        var a = Math.floor(5.9); // Return 5
        var b = Math.ceil(5.1); // Return 6
        var c = Math.round(5.4); // Return 5
        var d = Math.round(5.5); // Return 6

        document.write("a = " + a + "<br />");
        document.write("b = " + b + "<br />");
        document.write("c = " + c + "<br />");
        document.write("d = " + d + "<br />");
    </script>
</body>

</html>


Previous                                                 Next                                                 Home

JavaScript: parseFloat: Convert string to float

parseFloat(string)
parseFloat takes a string and return a float value.

Ex:
var a = parseFloat("123.45"); // Return 123.45
var b = parseFloat("0.987654"); // Return 0.987654

parseFloat converts the character by character, if it is unable to convert any character it ignores it and all succeeding characters and returns the float value parsed up to that point.

var c = parseFloat("123.45awq34"); // Return 123.45

If the first character cannot be converted to a number, parseFloat returns NaN.

var d = parseFloat("a1234"); // Return NaN

parseFloat.html
<!DOCTYPE html>

<html>

<head>
    <title>parseFloat</title>
</head>

<body>
    <script type="text/javascript">
        var a = parseFloat("123.45"); // Return 123.45
        var b = parseFloat("0.987654"); // Return 0.987654
        var c = parseFloat("123.45awq34"); // Return 123.45
        var d = parseFloat("a1234"); // Return NaN

        document.write("a = " + a + "<br />");
        document.write("b = " + b + "<br />");
        document.write("c = " + c + "<br />");
        document.write("d = " + d + "<br />");
    </script>
</body>

</html>



Previous                                                 Next                                                 Home

JavaScript: parseInt: Convert string to integer

parseInt(string, radix);
parseInt method takes a string and radix as arguments, and convert the string to integer in given radix.

var a = parseInt("0377", 8); // Return 255
var b = parseInt("255", 10); // Return 255
var c = parseInt("0xff", 16); // Return 255

parseInt converts the character by character, if it is unable to convert any character it ignores it and all succeeding characters, returns the integer value parsed up to that point.

var d = parseInt("123edf56", 10); // Return 123

If the first character cannot be converted to a number, parseInt returns NaN.

parseInt.html
<!DOCTYPE html>

<html>

<head>
    <title>parseInt</title>
</head>

<body>
    <script type="text/javascript">
        var a = parseInt("0377", 8); // Return 255
        var b = parseInt("255", 10); // Return 255
        var c = parseInt("0xff", 16); // Return 255

        var d = parseInt("123edf56", 10); // Return 123
        var e = parseInt("a1234", 10); // Return NaN

        var f = parseInt("11111111", 2); // Return 255

        document.write("a = " + a + "<br />");
        document.write("b = " + b + "<br />");
        document.write("c = " + c + "<br />");
        document.write("d = " + d + "<br />");
        document.write("e = " + e + "<br />");
        document.write("f = " + f + "<br />");
    </script>
</body>

</html>



Previous                                                 Next                                                 Home

Tuesday, 28 August 2018

JavaScript: NaN: Not a Number

NaN stands for Not a Number, it is returned by Javascript when mathematical functions fail to return the result (or) when a function trying to parse a number fails.

var a = Math.sqrt(-1); // Return NaN
var b = parseInt("abrakadabra");  // Return NaN

Some Points to remember
a.   Zero divided by Zero results NaN
b.   Infinity divided by Inifnity results NaN
c.   Any Arithmetic operation on non-numeric operands that can’t be converted to number type leads to NaN.

How to test whether given variables value is NaN or not?
Javascript provides special function isNaN(variable), it return true if the given variable is NaN, else false.

nan.html

<!DOCTYPE html>

<html>

<head>
    <title>NaN example</title>
</head>

<body>
    <script type="text/javascript">
        var a = Math.sqrt(-1); // Return NaN
        var b = parseInt("abrakadabra"); // Return NaN
        var c = 10.09;

        document.write("a = " + a + "<br />");
        document.write("b = " + b + "<br />");
        document.write("c = " + c + "<br />");

        document.write("isNaN(a) = " + isNaN(a) + "<br />"); // Return true
        document.write("isNaN(b) = " + isNaN(b) + "<br />"); // Return true
        document.write("isNaN(c) = " + isNaN(c) + "<br />"); // Return false
    </script>
</body>

</html>

Note
a. If you perform any arithmetic operation with NaN, then the result is NaN.



Previous                                                 Next                                                 Home

JavaScript: Working with Infinity

Javascript provides properties Infinity, -Infinity to support mathematical infinity. When the result of any arithmetic operation is greater than the maximum number represented by JavaScript, then it is treated as Infinity. When the result of any arithmetic operation is less than the minimum number represented by JavaScript, then it is treated as -Infinity.

Some points to remember
a. Any positive number divide by zero leads to Infinity.
b. Any negative number divide by zero leads to -Infinity.
c. Any positive number multiplied by Infinity is Infinity
d. Anything divided by Infinity is 0.

Ex:
var a = 10/0;      // Infinity
var b = -10/0;     // -Infinity
var c = 10 * Infinity; // Infinity
var d = 10 * -Infinity; //  -Infinity
var e = 10/Infinity;       // 0
var f = 10/-Infinity;      // 0

infinity.html
<!DOCTYPE html>

<html>

<head>
    <title>Infinity</title>
</head>

<body>
    <script type="text/javascript">
        var a = 10 / 0; // Infinity
        var b = -10 / 0; // -Infinity
        var c = 10 * Infinity; // Infinity
        var d = 10 * -Infinity; //  -Infinity
        var e = 10 / Infinity; // 0
        var f = 10 / -Infinity; // 0

        document.write("a = " + a + "<br />");
        document.write("b = " + b + "<br />");
        document.write("c = " + c + "<br />");
        document.write("d = " + d + "<br />");
        document.write("e = " + e + "<br />");
        document.write("f = " + f + "<br />");
    </script>
</body>

</html>




Previous                                                 Next                                                 Home

JavaScript: Working with numbers

Javascript don't have specific type for integers and float values. All the numbers are of type ‘number’. Internally these numbers represented in double precision. Javascript number takes 8 bytes memory.

var a = 10; //typeof a return number
var b = 10.02;     //typeof b return number

Different ways to represent numbers
In addition to representing numbers in decimal form, you can alslo represent numbers in octal, hexa and scientific notations.

Octal notation
Numbers in octal notation represented by 0 followed by the number.

Ex:
0123 is equal to 83 in decimal

Hexa notation
In Hexa form number is preceded by ox.

Ex:
0x53 is equal to 83 in decimal.

Scientific notation
You can also represent numbers in scientific notation.

Syntax
[digits][.digits][(E|e)[(+|-)]digits]

Ex:
8.3e1 = 8.3 + (10 power 1) = 83
0.83e2 = 0.83 * (10 power 2) = 83

numbers.html
<!DOCTYPE html>

<html>

<head>
    <title>Numbers</title>
</head>

<body>
    <script type="text/javascript">
        var a = 10; //typeof a return number
        var b = 10.02; //typeof b return number

        var c = 83;
        var d = 0123; // 83 in octal
        var e = 0x53; // 83 in hexa
        var f = 8.3e1; // 83 in scientific notation

        document.write("a = " + a + ", type = " + typeof(a) + "<br />");
        document.write("b = " + b + ", type = " + typeof(b) + "<br />");

        document.write("c = " + c + "<br />");
        document.write("d = " + d + "<br />");
        document.write("e = " + e + "<br />");
        document.write("f = " + f + "<br />");
    </script>
</body>

</html>

Note

ECMAScript standard don’t support numbers in octal form, so better not to use them in your scripts.


Previous                                                 Next                                                 Home