Javascript testing for true
Shows that 'undefined' null and empty string ('') will return false when tested for as I would expect. Also messed around with some CSS...
by Matt Rummler
HTML
<section>
Some stuff just cause..., though no HTML is needed...
</section>
CSS
html
{
width: 100%;
height: 100%;
}
html, section
{
margin: 0 auto;
}
section
{
position: absolute;
display: block;
bakground: #F1FEFF;
left: 10%;
width: 80%;
height: 80%;
box-shadow: 2px 2px 8px #CADFF1;
}
JavaScript
alert('@ the beginning');
var TestObject={testZero:0,testIsString:'1st',testIsNull:null,testIsEmptyStr:'',testInt:1,testEmptyArray:[]};
//1st test = zero test
var tested=TestObject['testZero'];
alertIfTrue(tested,'testZero');
//2nd test = string test
var tested=TestObject['testIsString'];
alertIfTrue(tested,'testIsString');
//3rd test = null test
tested=TestObject['testIsNull'];
alertIfTrue(tested,'testIsNull');
//4th test = empty string test
tested=TestObject['testIsEmptyStr'];
alertIfTrue(tested,'testIsEmptyStr');
//5th test = undefined test
tested=TestObject['doesntExist'];
alertIfTrue(tested,'doesntExist');
//6th test = int test
tested=TestObject['testInt'];
alertIfTrue(tested,'testInt');
//7th test = empty array test
tested=TestObject['testEmptyArray'];
alertIfTrue(tested,'testEmptyArray');
//8th test = empty object test
tested={};
alertIfTrue(tested,'testEmptyObject');
//9th test = negative 1 -1
tested=-1;
alertIfTrue(tested,'testNegativeOne');
function alertIfTrue(propertyValue,propertyName)
{
propertyName=propertyName || 'No property name was passed';
if(propertyValue)
{
alert('The value of TestObject.'+propertyName+' has resolved to true and has the following value: '+propertyValue);
}
else
{
alert('The value of TestObject.'+propertyName+' has resolved to false');
}
}
alert('@ the end');