retrievedID is a string returned by window localStorage property in my code as:
$(function () {
var retrievedID = localStorage.getItem('navitemID');
if ($(retrievedID).val() !== '') {
$('.navbar-nav li').removeClass('active');
var myID = "#" + retrievedID;
$(myID).addClass('active');
}
});
if statement in this code isn't working as expected because despite having an empty string inside recievedID variable, code is still entering inside the condition and executing.
Can someone please help. Thanks in advance.
$(retrievedID).val()? Anything other than an empty string will satisfy the condition.$(retrievedID).val()in console is coming as undefinedundefined !== ''will evaluate to trueifcondition in Mandeep's solution is always coming as true for all values and hence code inside the if condition is never getting to execute.