JavaScript Undefinded problem...
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Undefinded problem...

  1. #1
    Join Date
    Mar 2006
    Posts
    228

    Question Undefinded problem...

    Hello,

    I have the following JavaScript:

    Code:
    var ev_index = "001";
    
    if (ev_index == "001")
      {
      return "hello";
      }
    if the var ev_index does not = to 001 I want it to exit and not do anything.

    at the minute it returns undefinded.

    Does anyone know how to just exit the if statement without returning anything.

    Thanks.

  2. #2
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,860

    Re: Undefinded problem...

    Only functions can return a value. You are looking for break.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  3. #3
    Join Date
    Mar 2006
    Posts
    228

    Re: Undefinded problem...

    Hello,

    I have now done the following:

    Code:
    var ev_index = "001";
    
    if (ev_index == "001")
    {
    	return "hello";
    }
    else
    {
    	break;
    }
    If the ev_index = 001 it works fine

    but, If the ev_index = any other number it comes up with a Syntax error. (invalid break statement)

    Am I using the break correct ?

    Aaron.

  4. #4
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,860

    Re: Undefinded problem...

    No. It is not correct. As I said in my previous post, you cannot use return in a in if() statement. Only functions have returns. Hence why you are getting an undefined return.

    But, why even bother checking the positive if you only want to check for a negative? Save yourself some code.

    Code:
    if (ev_index != "001") {
    	// do processing here
    }
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center