2018-10-04 14:47:55 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								title: Falsy Values
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## Description
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-11-07 13:38:56 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								A falsy value is something which evaluates to FALSE, for instance when checking a variable. There are only six falsy values in JavaScript: `undefined` , `null` , `NaN` , `0` , `""`  or `''`  (empty string), and the Boolean `false`  of course. All other values are [truthy ](https://github.com/freeCodeCamp/freeCodeCamp/edit/master/guide/english/javascript/truthy-values/index.md ).
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:47:55 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## Checking for falsy values on variables
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								It is possible to check for a falsy value in a variable with a simple conditional:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```javascript
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								if (!variable) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  // When the variable has a falsy value the condition is true.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-11-07 13:38:56 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								You can also get the boolean value of a variable by using the bang operator (`!` ) twice:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```javascript
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								!!variable // When the variable is falsy, a double bang (!!) will evaluate to the Boolean false.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:47:55 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								## General Examples
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```javascript
							 
						 
					
						
							
								
									
										
										
										
											2018-10-19 01:59:40 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								const string = ""; // < --  falsy 
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:47:55 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-10-19 01:59:40 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								const filledString = "some string in here"; // < --  truthy 
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:47:55 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-10-19 01:59:40 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								const zero = 0; // < --  falsy 
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:47:55 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-10-19 01:59:40 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								const numberGreaterThanZero; // < --  falsy 
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:47:55 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-10-19 01:59:40 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								const emptyArray = []; // < --  truthy ,  we ' ll  explore  more  about  this  next 
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:47:55 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-10-19 01:59:40 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								const emptyObject = {}; // < --  truthy 
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:47:55 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## Fun With Arrays
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```javascript
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								if ([] == false) // < --  truthy ,  will  run  code  in  if-block 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								if ([]) // < --  truthy ,  will  also  run  code  in  if-block 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								if ([] == true) // < --  falsy ,  will  NOT  run  code  in  if-block 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								if (![]) // < --  falsy ,  will  also  NOT  run  code  in  if-block 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## Caveat
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-10-30 13:43:10 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Be aware of the data type when evaluating a value in a Boolean context. If the data type of the value is meant to be a _number_ , the truthy/falsy evaluation can result in an unexpected outcome:
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:47:55 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								```javascript
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const match = { teamA: 0, teamB: 1 }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								if (match.teamA)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  // The following won't run due to the falsy evaluation
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  console.log('Team A: ' + match.teamA);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								An alternative to the use case above is to evaluate the value using `typeof` :
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```javascript
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const match = { teamA: 0, teamB: 1 }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								if (typeof match.teamA === 'number')
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  console.log('Team A: ' + match.teamA);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## More Information
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								-  < a > **truthy**</ a >  | < a  href = 'http://james.padolsey.com/javascript/truthy-falsey/'  target = '_blank'  rel = 'nofollow' > Blog Post - Truthy &  Falsey</ a >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								-  [ Falsy | Glossary | MDN ](https://developer.mozilla.org/en-US/docs/Glossary/Falsy ) 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								-  [Truthy and Falsy: When All is Not Equal in JavaScript ](https://www.sitepoint.com/javascript-truthy-falsy/ )