Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

how to check if a variable is true or false in javascript

/* 
  A variables value can be an actual boolean like "true" / "false"
  or it can be of another type like string or array which can be evaluated as "truthy" / "falsy".
  Truthy: https://developer.mozilla.org/de/docs/Glossary/Truthy
  Falsy: https://developer.mozilla.org/de/docs/Glossary/Falsy
*/

// Independent check
if (myVar) {
	// entered if myVars value is either a boolean of value "true" or a value evaluated as "truthy"
}

// true/false check
if (typeof myVar === "boolean" && myVar) {
	// entered if myVars value is an actual boolean and if this boolean is "true"
}

// truthy/falsy check
if (typeof myVar !== "boolean" && myVar) {
	// entered if myVars value is not an actual boolean but if it's evaluated as "truthy"
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: samoglasnici-vowels 
Javascript :: polymer js tutorial 
Javascript :: how to make pdf of web page 
Javascript :: How do I use for-loops js 
Javascript :: how to create object dynamically in javascript 
Javascript :: pagination in b table in bootstrap vue 
Javascript :: how to add multiple objects into array in javascript 
Javascript :: how to upgrade nodejs version 
Javascript :: project to do with javascript 
Javascript :: quiz javascript 
Javascript :: Search by text score in mongodb 
Javascript :: Geometery parsing GeoJSON 
Javascript :: binding style vuejs 
Javascript :: javascript classes 
Javascript :: how reducer works in redux 
Javascript :: bind() in javascript 
Javascript :: date time react component 
Javascript :: how to set array in javascript 
Javascript :: what is heap in javascript 
Javascript :: number , number methods in js 
Javascript :: timer js 
Javascript :: status discored jks 
Javascript :: google chart ajax json 
Javascript :: delete class through the dom 
Javascript :: Differences between detach(), hide() and remove() - jQuery 
Javascript :: npm i react-router semantic-ui-react semantic-ui-css 
Javascript :: tskill nodejs port 
Javascript :: queryselect get type of elment class or id 
Javascript :: add variable to nth child jquery 
Javascript :: nodejs json data serving 
ADD CONTENT
Topic
Content
Source link
Name
6+6 =