Tag Archives: Raw

Use of !! in JavaScript

BANG BANG

Want to find out if something is false if it is a string, number, obj etc….


var x = “No”
typeof x
// string

var xBools = !!x;
//true – because variable x has a string of No, could be a string of false as well. A string is an object and that object has alpha characters
typeof xBools
// boolean

var emptyString = “”;
typeof emptyString 
// string

var emptyStringBools = !!emptyString;
//false – because the string is empty
typeof emptyStringBools 
// Boolean

var nulls = null;
typeof nulls
// object

var nullsBools = !!nulls;
//false
typeof nullsBools
// boolean

var under;
typeof under
// undefined

var underBools = !!under;
//false
typeof underBools
// boolean

Check to see if a variable has a value and if that value is usable.