isNAN function in javascript

isNAN function checks the argument and returns true if the argument is not a number else return false


Example of isNAN()

var val='04cs';

if(isNAN(val))
{
alert('this is not a number') ;
}else{
alert('this is a number') 

}


ANS : this is not a number


var value='0411';

if(isNAN(value))
{
alert('this is not a number') ;
}else{
alert('this is a number') 

}


ANS : this is a number

undefined value and null value

undefined means a variable has been declared but has not yet been assigned a value. On the other hand, null is an assignment value. It can be assigned to a variable as a representation of no value.

Also, undefined and null are two distinct types: undefined is a type itself (undefined) while null is an object.


Unassigned variables are initialized by JavaScript with a default value of undefined. JavaScript never sets a value to null. That must be done programmatically.

undefined check not working in javascript

Checking undefined is not work when we go normal if statement, As like below

var id=$('#txt').val();

if(id != 'undefined')
{
// Our Code
}

"id" is var type, i.e mean unknown type, such as whether "id" is string, numeric, or even undefined.  
if "txt" is not defined in the page, then id's value is  undefined, so we shall check the type of operand with value.

By using "typeof" operator we resolve above issue. The "typeof" operator in JavaScript/Jquery allows you to probe the data type of its operand.

var item = 10;
alert(typeof item); This returns the type of item not value.


if(typeof id != 'undefined')
{
// Our Code


I hope this will help you to resolve the problem.

How to check if a variable is NOT undefined

Checking undefined is not work when we go normal if statement, As like below

var id=$('#txt').val();

if(id != 'undefined')
{
// Our Code
}

"id" is var type, i.e mean unknown type, such as whether "id" is string, numeric, or even undefined.  
if "txt" is not defined in the page, then its value is  undefined is a one of type, so we shall check the type of operand with value.

By using "typeof" operator we resolve above issue. The "typeof" operator in JavaScript/Jquery allows you to probe the data type of its operand.

var item = 10;
alert(typeof item); This returns the type of item not value.


if(typeof id != 'undefined')
{
// Our Code


I hope this will help you to resolve the problem.

What's the main difference between int.Parse() and Convert.ToInt32

string str = "199"

int.Parse(str)

  1. This method converts the string to integer. 
  2. If string variable 'str' is null, then it will throw ArgumentNullException. 
  3. If string 'str' has out of integer ranges, then it will throw OverflowException.
  4. If string 'str' is other than integer value, then it will throw FormatException. 


Convert.ToInt32(str)

  1. This method converts the string to integer. 
  2. If string str is null, then it will return 0 rather than throw ArgumentNullException.
  3. If string str represents out of integer ranges, then it will throw OverflowException. 
  4. If string str is other than integer value, then it will throw FormatException. 


What's the main difference between int.Parse() and Convert.ToInt32

string str = "199"

int.Parse(str)

  • This method converts the string to integer. 
  • If string variable 'str' is null, then it will throw ArgumentNullException. 
  • If string 'str' has out of integer ranges, then it will throw OverflowException.
  • If string 'str' is other than integer value, then it will throw FormatException. 


Convert.ToInt32(str)

  • This method converts the string to integer. 
  • If string str is null, then it will return 0 rather than throw ArgumentNullException.
  • If string str represents out of integer ranges, then it will throw OverflowException. 
  • If string str is other than integer value, then it will throw FormatException.