js: value is null, typeof returns string, but comparison with null or '' returns false





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















Am evaluating koa js for a solution, but ran through the following issue:



Have some http headers which need not always be populated by the client, means sometimes the header will be null, but on processing the header on server side comparing it for null/empty string always returns false even when it is null.



To see whats the value and what the outcome of comparison is, have traced all the possible outcomes and here are the following:



console.log(typeof ctx.get('X-SessionId'));
console.log(ctx.get('X-SessionId'));
console.log(!ctx.get('X-SessionId'));
console.log(ctx.get('X-SessionId') === null);
console.log(ctx.get('X-SessionId') == null);
console.log(ctx.get('X-SessionId') == '');
console.log(ctx.get('X-SessionId') === '');
console.log(ctx.get('X-SessionId').length);


Output :



string
null
false
false
false
false
false
4


Here the typeof returns string but the value itself is null. Comparing it with either null or '' returns false. And surprisingly .length returns 4!



Anyone has a suggestion how to overcome this comparison problem?










share|improve this question


















  • 1





    literal "null" = 4 chars? Try console.log(ctx.get('X-SessionId') === "null");

    – Emma
    Jan 3 at 6:01













  • Oh my God yes its literal 'null'. Cross checked whats being passed from client side. But there it says null and not "null". Normally any string thats passed on headers are contained in "", so am pretty sure the header is passed as null, Wondering why on the server side its taking as "null"? Anyways thanks a lot @Emma for this suggestion.

    – Rayees Km
    Jan 3 at 6:12




















0















Am evaluating koa js for a solution, but ran through the following issue:



Have some http headers which need not always be populated by the client, means sometimes the header will be null, but on processing the header on server side comparing it for null/empty string always returns false even when it is null.



To see whats the value and what the outcome of comparison is, have traced all the possible outcomes and here are the following:



console.log(typeof ctx.get('X-SessionId'));
console.log(ctx.get('X-SessionId'));
console.log(!ctx.get('X-SessionId'));
console.log(ctx.get('X-SessionId') === null);
console.log(ctx.get('X-SessionId') == null);
console.log(ctx.get('X-SessionId') == '');
console.log(ctx.get('X-SessionId') === '');
console.log(ctx.get('X-SessionId').length);


Output :



string
null
false
false
false
false
false
4


Here the typeof returns string but the value itself is null. Comparing it with either null or '' returns false. And surprisingly .length returns 4!



Anyone has a suggestion how to overcome this comparison problem?










share|improve this question


















  • 1





    literal "null" = 4 chars? Try console.log(ctx.get('X-SessionId') === "null");

    – Emma
    Jan 3 at 6:01













  • Oh my God yes its literal 'null'. Cross checked whats being passed from client side. But there it says null and not "null". Normally any string thats passed on headers are contained in "", so am pretty sure the header is passed as null, Wondering why on the server side its taking as "null"? Anyways thanks a lot @Emma for this suggestion.

    – Rayees Km
    Jan 3 at 6:12
















0












0








0








Am evaluating koa js for a solution, but ran through the following issue:



Have some http headers which need not always be populated by the client, means sometimes the header will be null, but on processing the header on server side comparing it for null/empty string always returns false even when it is null.



To see whats the value and what the outcome of comparison is, have traced all the possible outcomes and here are the following:



console.log(typeof ctx.get('X-SessionId'));
console.log(ctx.get('X-SessionId'));
console.log(!ctx.get('X-SessionId'));
console.log(ctx.get('X-SessionId') === null);
console.log(ctx.get('X-SessionId') == null);
console.log(ctx.get('X-SessionId') == '');
console.log(ctx.get('X-SessionId') === '');
console.log(ctx.get('X-SessionId').length);


Output :



string
null
false
false
false
false
false
4


Here the typeof returns string but the value itself is null. Comparing it with either null or '' returns false. And surprisingly .length returns 4!



Anyone has a suggestion how to overcome this comparison problem?










share|improve this question














Am evaluating koa js for a solution, but ran through the following issue:



Have some http headers which need not always be populated by the client, means sometimes the header will be null, but on processing the header on server side comparing it for null/empty string always returns false even when it is null.



To see whats the value and what the outcome of comparison is, have traced all the possible outcomes and here are the following:



console.log(typeof ctx.get('X-SessionId'));
console.log(ctx.get('X-SessionId'));
console.log(!ctx.get('X-SessionId'));
console.log(ctx.get('X-SessionId') === null);
console.log(ctx.get('X-SessionId') == null);
console.log(ctx.get('X-SessionId') == '');
console.log(ctx.get('X-SessionId') === '');
console.log(ctx.get('X-SessionId').length);


Output :



string
null
false
false
false
false
false
4


Here the typeof returns string but the value itself is null. Comparing it with either null or '' returns false. And surprisingly .length returns 4!



Anyone has a suggestion how to overcome this comparison problem?







node.js koa






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 3 at 5:56









Rayees KmRayees Km

32




32








  • 1





    literal "null" = 4 chars? Try console.log(ctx.get('X-SessionId') === "null");

    – Emma
    Jan 3 at 6:01













  • Oh my God yes its literal 'null'. Cross checked whats being passed from client side. But there it says null and not "null". Normally any string thats passed on headers are contained in "", so am pretty sure the header is passed as null, Wondering why on the server side its taking as "null"? Anyways thanks a lot @Emma for this suggestion.

    – Rayees Km
    Jan 3 at 6:12
















  • 1





    literal "null" = 4 chars? Try console.log(ctx.get('X-SessionId') === "null");

    – Emma
    Jan 3 at 6:01













  • Oh my God yes its literal 'null'. Cross checked whats being passed from client side. But there it says null and not "null". Normally any string thats passed on headers are contained in "", so am pretty sure the header is passed as null, Wondering why on the server side its taking as "null"? Anyways thanks a lot @Emma for this suggestion.

    – Rayees Km
    Jan 3 at 6:12










1




1





literal "null" = 4 chars? Try console.log(ctx.get('X-SessionId') === "null");

– Emma
Jan 3 at 6:01







literal "null" = 4 chars? Try console.log(ctx.get('X-SessionId') === "null");

– Emma
Jan 3 at 6:01















Oh my God yes its literal 'null'. Cross checked whats being passed from client side. But there it says null and not "null". Normally any string thats passed on headers are contained in "", so am pretty sure the header is passed as null, Wondering why on the server side its taking as "null"? Anyways thanks a lot @Emma for this suggestion.

– Rayees Km
Jan 3 at 6:12







Oh my God yes its literal 'null'. Cross checked whats being passed from client side. But there it says null and not "null". Normally any string thats passed on headers are contained in "", so am pretty sure the header is passed as null, Wondering why on the server side its taking as "null"? Anyways thanks a lot @Emma for this suggestion.

– Rayees Km
Jan 3 at 6:12














0






active

oldest

votes












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54017027%2fjs-value-is-null-typeof-returns-string-but-comparison-with-null-or-returns%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54017027%2fjs-value-is-null-typeof-returns-string-but-comparison-with-null-or-returns%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

SQL update select statement

'app-layout' is not a known element: how to share Component with different Modules