0

I am not so expert in JavaScript and I am wonder if there is a way to test the boolean value of a variable in javascript.

In Python I can do this:

>>>list_var = []
>>>bool(list_var)
False  # This is the boolean value of a empty list
>>>

And if I try get an element in JS that does not exist, i.e:

document.getElementById('b-advanced')
[]  // This is what returns

Is there a way to test the expression above as boolean without using an if... statement?

EDIT

I think I need to point something.

This is the full expression I use:

angular.element(document.getElementById('b-advanced'))
4
  • 2
    getElementById would never return an array. It either returns a DOM element or null. Commented Jul 14, 2015 at 21:12
  • Well, I am using that expression inside a angular.element() function. Commented Jul 14, 2015 at 21:13
  • How does that make any difference? Commented Jul 14, 2015 at 21:16
  • You are rigth, they don't return the same. postimg.org/image/wix57vk55 Commented Jul 14, 2015 at 21:22

5 Answers 5

2

There is not. The best you can do is list_var.length === 0. For a full test, you'd want to test as follows

// Return true if arg is an array or string containing at least one item
function isTrueness(arg) {
    return !!(arg && arg.length > 0);
}

angular.element(isTrueness(document.getElementById('b-advanced')));
Sign up to request clarification or add additional context in comments.

1 Comment

this a good approach. I come from python and does not make sense for me that an empty array is equivalent to true.
1

You can use !! in front which will return its boolean status.

!![] // true
!!null // false

5 Comments

So, an empty array is equivalent to true?
Correct! See Conflicting boolean values of an empty Javascript Array for a great explanation of this :)
This is really unexpected for me. Thanks for your answer.
If you found this answer to be helpful, please consider accepting it. Learn more about accepting answers here.
I think this is the simplest way. No functions, no if statements.
1
var x = Boolean(document.getElementById('b-advanced'));

Boolean is a wrapper for boolean values.

Comments

0

you can test to see what type a variable is by using typeof so for example:

var myVar = true;
if (typeof myVar === "boolean") console.log("It is a boolean");

6 Comments

Note that, for some reason, the OP specifically wants to avoid using an if statement. However, I'm not sure this is a possibility. Anyway, if(emptyVar) will equate to false if the variable is empty by the various loose definitions JS employs.
Thanks for your answer, but that's not exactly what I am asking for. In JS this: [] is equivalent to false?
I see... you're using getElementyById to get that value. Which as pointed out by someone else, would only return either null or a DOM element so i'm not sure in which scenario it would even return an array which is what [] is in JS
@Gocht: every object is "truthy" in JavaScript, so no.
are you maybe trying to get the text or an attribute of the element?
|
0

In Python, an empty list is false. In Javascript, an empty array is true. You can mimic this by saying myArray.length && myArray. If myArray is empty, then its length is zero and the expression is false. If myArray isn’t empty, then Javascript returns the second operand, just like Python.

Comments

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.