1

I have some calls to AJAX where I return a JSON object as a response. This JSON object always has the same structure. However, there are sometimes where if the PHP scripts fails, some of the objects of the JSON structure are not defined or even inexistent.

This also happens with some "global" variables that are present in a page and I use in some JS files, but then go to another page where I use those JS files and the variable is not even written.

So, I thought to create a function is_defined() to which I could ask if a variable is defined or not. The function is as the following:

function is_defined(ob, can_empty){
    if(can_empty === undefined || can_empty == '') can_empty = false

    return ob !== undefined && ob !== null && (ob !== '' || can_empty)
}

So, if the variable passed is NULL or undefined, or it's empty (and it cannot be empty), I return false.

My problem comes when the variable is not even written, as I cannot pass it to the function because an Uncaught ReferenceError: obj is not defined error is raised.

So my question is if is there any way to create a function to check if a variable exists and is defined, as I'd like to centralize all these checks into a function and not writing obj !== undefined || obj !== null every now and then on my code.

I thought that with a double Not operator (!!!obj) would cast to false, but I face the same problem if the variable is not written.

EDIT: Just as an aclaration, the issue here is to be able to do the checks for undeclared and undefined variables inside a function, so I can pass a variable to the function without having to check it before calling it if the variable is declared or not.

Thank you all for your time and answers!

Kind regards!

0

3 Answers 3

2

[...] is there any way to create a function to check if a variable exists and is defined [...]

No there is not. When you pass a variable to a function, it will always try to resolve that variable. If it is not declared it will throw a reference error.

Relevant section from the ECMAScript specification.

You have to check whether the variable is declared via typeof before you pass it to a function.

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

1 Comment

Hi! Thanks for your answer! So, it seems to be that there is no way to do what I'd like... it's a pitty... Thank you so much for your time!
2

You can use typeof variableName === 'undefined' to test if the variable is even declared.

Specifically:

  • typeof variable === 'undefined' tests if the variable is declared.
  • variable === undefined tests if the declared variable is defined or assigned any value.

8 Comments

typeof does not take parameters . How is this correct? btw , it does in c#
@MohitBhasi: (...) is the grouping operator. Just like you can do !(x) instead of !x. It's a bit unusual, but not wrong.
@MohitBhasi yes, typeof is an operator and not a function but as Felix has pointed out (...) is not just used for function invocation.
Hello! Thanks for your answers! Although you are right to be able to check if a variable is declared with this way, it should be done before passing the variable to a function. So, although it's better than checking for undefined and null, I'd still had to write this piece of code in every check in the code. That's my aim: to be able to centralize it all the checks into a unique function, and with this solution I have the same problem. Thanks anyway!
@Adracat I have updated the answer which check if typeof ob === 'undefined'. Will this solve your purpose of having centralised function?
|
0

Foolproof way to check for an existing variable:

typeof variableName === 'undefined'

1 Comment

Hello! thanks for your answer, but as I pointed out in Anurag Peshne answer, I still have the same problem with this: I have to check it before calling the function "is_defined()". So, I'd still have to write it through all my code. Thanks for your time!

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.