31

Call to a JS function

alertStatement()

Function Definition

function alertStatement(link) {
    if (link) {
        alert('A');     
    }

    if (link!=null) {
        alert('B');     
    }   
}

Both of these statements are working fine in Windows Env with Tomcat, but none of them execute it on production (Linux server). Is there any other way to compare variables to make it working?

I got it working using the following javascript code.

function alertStatement(link) {
    if (link!==undefined){
        alert('A');     
    }
}

So at last undefined worked for me , for some reason null comparison didn't work

5
  • What symptom are you seeing that makes you think there is a problem? Commented Jul 12, 2012 at 22:10
  • Can you define "working fine"? What would you expect to happen? Commented Jul 12, 2012 at 22:18
  • Sorry For the confusion , When I call the function on windows without the parameter then both alert statements dont popup. In Linux I can see both alert boxes being popped up. Commented Jul 13, 2012 at 0:54
  • Unless you're using something like Node.js, JS is client side. What the server runs is a red herring. Commented Jul 14, 2012 at 22:59
  • outis : yes that makes a valid point , but I am not using the node.js .I used the alert(link) to see the value that is coming when a call is made with out parameter like alertstatement() , and the result I got was alert box with "undefined" in it so I compared with undefined which worked eventually Commented Jul 15, 2012 at 16:35

1 Answer 1

62

To see if the argument has a usable value, just check if the argument is undefined. This serves two purposes. It checks not only if something was passed, but also if it has a usable value:

function alertStatement(link) {
    if (link !== undefined) {
        // argument passed and not undefined
    } else {
        // argument not passed or undefined
    }
}

Some people prefer to use typeof like this:

function alertStatement(link) {
    if (typeof link !== "undefined") {
        // argument passed and not undefined
    } else {
        // argument not passed or undefined
    }
}

null is a specific value. undefined is what it will be if it is not passed.

If you just want to know if anything was passed or not and don't care what its value is, you can use arguments.length.

function alertStatement(link) {
    if (arguments.length) {
        // argument passed
    } else {
        // argument not passed
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

arguments.length === 0 indicates that an argument was not passed. The current code will execute the first branch when called via alertStatement(null) or alertStatement(undefined) because null == undefined.
@Mike - what problem are you trying to solve that my solution does not solve? I don't get the reason for downvote. I'm testing if the link argument was passed with a legitimate value or not.
For the OP's specific situation arguments.length would work fine, but this is a more general solution that can test if any specific argument has a good value and doesn't need maintenance if you or some other coder adds an argument sometime later. This also works fine so I'm still unsure why the downvote.
val !== null also may works

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.