Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if argument is passed to a Java Script function [closed]

Tags:

javascript

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

like image 365
KAPILP Avatar asked Jul 12 '12 22:07

KAPILP


1 Answers

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     } } 
like image 184
jfriend00 Avatar answered Oct 22 '22 17:10

jfriend00