Hello again @Harshit_Badolla.
As I understand it, ''
is an empty string value, whereas null
is an absense of any value. You’ll meet them in different contexts.
So for example, you might want to check whether a text field has a value filled in or not, and react accordingly, so something like:
if(textInput.value = '') {
alert('The name field is empty. Did you mean to include a name?');
}
You’ll most commonly come across null
when you try to grab a reference to something but it doesn’t exist. For example let’s say you want to print out the number of news articles in the headlines container on your site, or report that there are no news articles if none are shown. You could maybe do something like:
if (document.querySelector('.headlines .story') === null) {
console.log(`There are no news stories to read today.`);
} else {
let storyCount = document.querySelectorAll('.headlines .story').length;
console.log(`There are ${ storyCount } news stories to read today.`);
}
Does that help?