JavaScript Fundamentals: Checking for null and undefined

preview_player
Показать описание
The primitive values null and undefined have a distinct difference that should be used. But how to you tell the difference between a null value, an undefined value and other falsey values? We will address it in this tutorial.

Would you like to help keep this channel going?

Tutorials referred to in this video:

For more resources on JavaScript:

#javascript #AllThingsJavaScriptLLC
Рекомендации по теме
Комментарии
Автор

Null = intentional absence of value; undefined = unintentional is a very good distinction. Thanks for clearing that up!

edcoughlan
Автор

var val1, val2 = {}, val3 = ' ', val4 = 0; val5 = [];
if(val1 == null && val3.length === 0 && val5.length === 0 ) { console.log('value is absent -- val1, val 3 and val 5') };
if(Object.keys(val2).length === 0) { console.log('value is absent -- val2') };
if(val4 === 0) { console.log('value is absent -- val4') };

if the value is an empty string or empty array we can use length to find and if the object is an empty object we can use the Object inbuild method. Hope this covers all the scenarios

ashamula
Автор

This explains everything about undefined. Thanks a lot for clear explanation ^_^

fattah
Автор

or you could do: if(value != 0) then check for null/undefined.

Petar_Ral
Автор

Great tutorial! Could you make another one about !! operator?

GregoryGilelach
Автор

hey so like what ap do i use to write javascript ? i was using my notes on my macbook

artistunknown
Автор

Checking if a variable is = [ ] is also important (mainly when working with APIs).

var something = [ ] ;

if (something === undefined || something.length == 0) { }

luizmeier