0

In an Express app I have the following JS code (server side) in a middleware to check that a user provides an adequate token. This is only the relevant part for my question:

const authHeader = req.headers['authorization']
console.log("authHeader : ", authHeader)

if (authHeader === undefined) console.log("authHeader === undefined")
else console.log("authHeader !== undefined")

if (authHeader == undefined) console.log("authHeader == undefined")
else console.log("authHeader != undefined")

if (authHeader.valueOf() === undefined) console.log("authHeader === undefined")
else console.log("authHeader !== undefined")

if (authHeader.valueOf() == undefined) console.log("authHeader == undefined")
else console.log("authHeader != undefined")

if (typeof authHeader === 'undefined') console.log("authHeader === undefined")
else console.log("authHeader !== undefined")

if (typeof authHeader == 'undefined') console.log("authHeader == undefined")
else console.log("authHeader != undefined")

As one can see the code above does not do much, except getting an incoming parameter, and testing it, but here is the result in the server logs:

app[web.1]: authHeader :  undefined
app[web.1]: authHeader !== undefined
app[web.1]: authHeader != undefined
app[web.1]: authHeader !== undefined
app[web.1]: authHeader != undefined
app[web.1]: authHeader !== undefined
app[web.1]: authHeader != undefined

The console.log() shows that authHeader is undefined. But none of the following tests confirms that. They all show authHeader as not undefined.

Why is that ?

I have used all the possible ways I could think of to do the test but it seems they are all wrong.

P.S.

Knowing the way I do the test, I expect authHeader to be undefined.

What I do not expect is that none of the test would say so.

2
  • 2
    is it possible that the value is actually a string 'undefined'? Commented Nov 28, 2024 at 6:16
  • I just added this line "console.log("authHeader-JS : ", JSON.stringify(authHeader))" to my code and it confirmed your hint ! Commented Nov 28, 2024 at 6:57

0

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.