1

Can someone please explain this to me? I am attempting to do something if key != ''

var key = "";
if($('div[title="Keyword"]').text() != '') {
  key = $('div[title="Keyword"]').text().trim(); 
}

//this always alerts
if (key != '') { alert(key); } 

//this never alerts
if (!key) { alert(key); }
16
  • alert the value of key before the if and see what is it equal to. If it's not an empty string, then the first if will pass. Commented Oct 4, 2012 at 0:15
  • 1
    what does console.log(key) say? Commented Oct 4, 2012 at 0:16
  • 1
    You may have some invisible special character not removed by trim Commented Oct 4, 2012 at 0:17
  • 1
    I don't understand what's asked here. For any non-empty string in key, key != '' will evaluate to true, and !key will evaluate to false. Commented Oct 4, 2012 at 0:17
  • 1
    @user1689607 Good call. key.length is 1, key.charCodeAt(0) is 8203. I think I can solve this by checking if key.length > 1 Commented Oct 4, 2012 at 0:32

1 Answer 1

1

I said in the comments it might be an invisible character not stripped by trim. Well, after your further comments, it's clear that's the case. It's the U+200B Zero-width space character. It probably came from jsfiddle (I'm betting you pasted something from there).

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

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.