0

I want to check null and empty id in JavaScript function,but if syntax isn't work ?

var id = "<%=Request["Id"]%>";
 if (id !== "")
 if (id !== null)
{var id = "<%=new Guid(Request["ID"].ToString())%>";
 window.location = "/Controller/Action.aspx?Id=" + id; }

2 Answers 2

1

With javascript,

If you are trying to test for not-null (any value that is not explicitly NULL) this should work for you:

if( myVar !== null ) { 
    // your code
}

If you are only interested to test for not-empty (null value, zero number, empty string etc..) then try:

if( !myVar ) { 
    // your code
}

If you want to test for if a variable is defined at all (which I believe is what you are trying to achieve) then you can do it like:

if( typeof myVar !== 'undefined' ) { 
    // your code
}

Please let me know if it works for you.

Sign up to request clarification or add additional context in comments.

Comments

0

Read into binary logic:

var id = "<%=Request["Id"]%>";
if (id !== "" && id != null)  {
    var id = "<%=new Guid(Request["ID"].ToString())%>";
    window.location = "/Controller/Action.aspx?Id=" + id;
}

Then again, var id = "<%=Request["Id"]%>"; will never be null, only empty string, so perhaps you can drop that check altogether.

6 Comments

if (id !== "" && id != null)
Are you sure? It's valid JavaScript so perhaps it's ASP doing something?
I search about && syntax in java script and concluded that separated if syntax
Try with a regular html page, separate ifs may work with the braces in the right places but binary logic is far easier to read.
I try it but if condition passed. how can i see value of id ? with alert syntax?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.