Kevin Powell - CommunityKP-C
Kevin Powell - Communityβ€’2y agoβ€’
31 replies
snxxwyy

checking if a string is empty.

Hey, i had a string that started off with the value
""
and wanted to check if it was empty. I first tried
if (xyz.length <= 0) {...}
but found out that
""
equates to
null
and that it wouldn't work due to this. i researched it and found you could do
if (xyz)
which ended up working, this resource said that that tests for a value but i always thought that sort of if statement was for checking booleans (
if (xyz)
being "if xyz = true").

Is this a recommended way to check for an empty string and is that a recommended check to do if it's not checking for a boolean? thanks in advance.
Was this page helpful?