JavaScript


4
Apr 08

JavaScript parseInt and parseFloat functions

If the Number() function was generic in nature then the parseInt() and parseFloat() functions will act to be more specific in what they do.

parseInt() will parse the data into an integer value(if possible) and return the result. It returns ‘NaN’ i.e. ‘Not a Number’ if the data is unparseable.

Similarly, parseFloat() parses float values.

Here are a few examples to help you understand these two functions better:

var a = “10″;

alert(parseInt(a));

alert(parseFloat(a));

output:

10

10

a = “10.12″;

alert(parseInt(a));

alert(parseFloat(a));

output:

10

10.12

a = ” 10.25 as you can see, white spaces are allowed and also alphabets after the number”

alert(parseInt(a));

alert(parseFloat(a));

output
:

10

10.25

a = ” nope this is wrong, wont work 100.24″;

alert(parseInt(a));

alert(parseFloat(a));

output
:

NaN

NaN


1
Apr 08

‘indexOf’ or ‘contains’ function for Javascript

The thought of elaborating on the “contains” function for JavaScript occurred to me while I was writing my previous post . From what I know, JavaScript doesn’t seem to have a function to allow the user to check if a particular letter exists in the given data.

But, it does have an indexOf() function that might just do the trick :)

All you need to do is check whether the return value is -1 or not. A return value of -1 implies that the specific letter is not present in the given data.

A simple example below will make things clear:

function checkIndexOf()
{
var a = “hello”;

alert(a.indexOf(‘l’));
alert(a.indexOf(‘E’));
alert(a.indexOf(’0′));

}

The output is as follows:

2 // which implies that the letter ‘l’ is present in position 2.

-1 //which implies that the letter ‘E’ is not present.(JavaScript is case sensitive)

-1 //which implies that the number 0 is not present.


31
Mar 08

Javascript isNaN()

The isNaN() function is used to check if the data contained in the Javascript variable is a number or not. The function returns true if the data is not a number and false if the data is a number.

This function is of great help while performing javascript validation to check if a user has entered only numeric values into the textboxes as specified by you.

Here is an example:

var a = “hi”;

var b = “123″;

if(isNaN(a))

{

alert(“Variable ‘a’ is not a number.”);

}

if(isNaN(b))

{

alert(“Variable ‘b’ is not a number.”);

}

The output will be “Variable ‘a’ is not a number.”

A note to remember: “isNaN() ” will return false for “1e1″ .

I mean, if you have numeric data with the letter ‘e’ in it, then the data is still considered to be numeric!

why is that?

Well, cause it treats the value to be exponential. So if you plan to validate for a numeric field and do not want to allow the letter ‘e’ then remember to validate it separately.

Well, as far as validating ‘e’ is considered… I wonder how you do that in Javascript!!

I mean we need to have a function which will tell us if a particular letter exists in a given data. Hmmmm… Donno if there exists a function like that in javascript. My next post will be on this :)