Avatar of James Froggatt
James Froggatt
Flag for France asked on

In Javascript, what does "if (x) x =" mean?

I'm writing an auto-complete script little by little (html/ plain javascript/ php/ ajax).

I've come across the following code:

var x = document.getElementById(this.id + "autocomplete-list");
if (x) x = x.getElementsByTagName("div");

Open in new window


... and would like to understand this particular bit of the above code

What does the following bit mean? I've never seen logic like this before.

if (x) x  =  


Is this a comparison / some kind of logical operator? I'm wondering if it's a way to detect whether x is NULL and so get information from the immediate parent element. Nevertheless, I'd like to understand what it means.

If so, in 'long English', what does it mean?

Thank you
JavaScriptJSON

Avatar of undefined
Last Comment
James Froggatt

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
ste5an

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
James Froggatt

ASKER
Hello,

Ah ha!

So it's really saying;

"if the autocomplete-list currently defined as x exists, x is all the elements with tagname 'div'"

Is that correct?

If it is, you're a star, I've not come across a syntax option like the above before.

Thanks
James Froggatt

ASKER
I.e.

 if (x) // if x exists (is not a null value)
{
   x = x.getElementsByTagName("div");
}
James Froggatt

ASKER
Concise, to the point, clearly saw what I was having difficulty with. Spot on!

Thank you ste5an
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Michel Plungjan

The test is using the principle of "truthy" and "falsy" and will coerce them into true or false

The code you posted may be misleading later since it tests that x is set to something, anything instead of testing that the assignment returned a collection as expected

var x = document.getElementById(this.id + "autocomplete-list"); // returns an DOM node or undefined/null if nothing exists
if (x) x = x.getElementsByTagName("div"); // test that the previous returned something and then find the divs inside it

Open in new window


It luckily works, but if we convert it to jQuery for example, we are better off testing the length since $x will be a jQuery object regardless and you will get the error further down

var $x = $("#"+this.id + "autocomplete-list"); // returns an jQuery object regardless of existence of a list
if ($x.length >0) $x = $("div",$x); // better test

Open in new window


In JS the following are considered false in a conditional

* false (boolean)
* 0 (number)
* 0n (BigInt)
* NaN (Not a Number)
* Empty string
* undefined
* null

whereas true will be whatever is not falsy - even empty objects

A complete run down is found here

https://medium.com/coding-at-dawn/what-are-falsy-values-in-javascript-ca0faa34feb4
James Froggatt

ASKER
Michel,

Thank you for your further comments, I've learnt alot here!

Best wishes
James