Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is "undefined" equal to "null" in JavaScript?

Tags:

javascript

I've been watching some videos from Tut+ about Js.They said that sometimes "undefined" is equal to "null". So, when does this happen ?

like image 552
Tri nguyen minh Avatar asked Feb 09 '23 02:02

Tri nguyen minh


2 Answers

undefined == null
// => true

undefined === null
// => false

== tests for equality, === tests for identity (or strict equality). If in doubt, use ===.

like image 163
Amadan Avatar answered Apr 19 '23 23:04

Amadan


Just to add on , This question is somehow answered already, check here

You can just check if the variable has a truthy value or not. That means

 if( value ) {

 }

will evaluate to true if value is not:

null

undefined

NaN

empty string ("")

0

false
like image 32
Milad Rezazadeh Avatar answered Apr 19 '23 23:04

Milad Rezazadeh