Skip to content
Advertisement

Typeof comparison in Javascript

Why is typeof stringVariable not equal to String;

console.log(typeof 'hello' === String) // returns false

Why does js behave like this?

let arr1 = ['nabeel', 'aron', 123, true]
    // find
let result = arr1.find(function(item, index, array) {
    console.log(typeof item, typeof item === String)
    return (typeof item === String)
})
console.log("[Find()] String type item exists : ", result)

Advertisement

Answer

typeof ‘hello’ will return as ‘string’ (as string type) typeof 5 will return as ‘number’ (as a string)

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement