You got that right. Whenever we try to access an object property that doesn't exists, we get undefined. So we can use this feature to check if an object property exists or not. For example,
let str = "The quick brown fox"
let freq = {}
str.split("").forEach(char => {
freq[char] === undefined ? freq[char] = 1 : freq[char] += 1
})
console.log(freq)
This little piece of code makes JS objects behave like hashmaps and count the character frequency in the str string.
If you call a property/method and it doesn't exist, it returns undefined:
const myObject = { city: "Madrid", greet: function () { console.log(`Greetings from ${this.city}`); } } myObject.greet(); // Ok: Greetings from Madrid myObject.country; // undefinedHowever, the prototype chain ends when null is found:
Object.prototype // nullThat is a little weird for me, but I got it :)
Adib Hasib, thanks for the great post. I was studying it today so its nice to read it here ✨🎉