Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript truthy values


// this alls are truthy values including empty object and array,
// excluding empty string (empty string is falsy value)
if ([]) {
  console.log('✅ This runs');
}

if ({}) {
  console.log('✅ This runs');
}

if (true) {
  console.log('✅ This runs');
}

if ('test') {
  console.log('✅ This runs');
}
Comment

javascript truthy values and falsy values

// Truthy values

/*
	true
    {}
    []
    42
    "0"
    "false"
    new Date()
    -42
    12n
    3.14
    -3.14
    Infinity
    -Infinity
*/


// Falsy values

/*
  false
  0
  -0
  0n
  "", '', ``
  null
  undefined
  NaN
*/
Comment

Truthy and Falsy js

//Checking truthy and falsy value

function truthyOrFalsy (val) { 
    if(val) {
      return true
    } else {
      return false
    }
}

console.log(truthyOrFalsy(0)) // print false
console.log(truthyOrFalsy(5)) // print true
Comment

PREVIOUS NEXT
Code Example
Javascript :: TypeError: navigation.getParams is not a function. 
Javascript :: add in to array mongoose 
Javascript :: javascript how to remove the last character of the string 
Javascript :: jquery multiple ids same funjquery apply function to multiple elementsction 
Javascript :: svg to png base64 javascript 
Javascript :: create-react-app npm yarn 
Javascript :: java script zip function 
Javascript :: make an object javascript 
Javascript :: jquery: finding all the elements containing the text present in an array 
Javascript :: p5js left mouse click 
Javascript :: buffer nodejs 
Javascript :: import in react js 
Javascript :: arrow functions 
Javascript :: vue computed 
Javascript :: line separator with text in the center react native 
Javascript :: call python function from javascript 
Javascript :: redux toolkit how to set empty initial state 
Javascript :: javascript things to remember 
Javascript :: javascript round 
Javascript :: js stringify 
Javascript :: try catch javascript 
Javascript :: loop inside react js 
Javascript :: javascript multidimensional array 
Javascript :: next js environment variables 
Javascript :: uppercase first letter js 
Javascript :: cancel an event in javascript 
Javascript :: jspdf reduce size file 
Javascript :: react concatenate string and component 
Javascript :: javascript print to pdf 
Javascript :: flatten nested json objects 
ADD CONTENT
Topic
Content
Source link
Name
8+9 =