wrkbrs

[Javascript] if 조건 값에 대한 true, false 본문

JS

[Javascript] if 조건 값에 대한 true, false

zcarc 2019. 8. 4. 04:48

Do not confuse the primitive Boolean values true and false with truthiness or falsiness of the Boolean object. Any value that is not false, undefined, null, 0, -0, NaN, or the empty string (""), and any object, including a Boolean object whose value is false, is considered truthy when used as the condition. For example:

 

var b = new Boolean(false);

if (b) // this condition is truthy

 

 

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else

 

if...else

The if statement executes a statement if a specified condition is truthy. If the condition is falsy, another statement can be executed.

developer.mozilla.org

 

 


// Start: if에 들어가는 해당 값에 대한 true, false


if(0){
    console.log('number 0 is true');
} else {
    console.log('number 0 is false');
}

if(-0){
    console.log('number -0 is true');
} else {
    console.log('number -0 is false');
}

if(null){
    console.log('null is true');
} else {
    console.log('null is false');
}

if(undefined){
    console.log('undefined is true');
} else {
    console.log('undefined is false');
}

if(NaN){
    console.log('NaN is true');
} else {
    console.log('NaN is false');
}

if("") {
    console.log('empty string(\"\") is true');
} else {
    console.log('empty string(\"\") is false');
}


if(1) {
    console.log('1 number is true');
} else {
    console.log('1 number is false');
}

var b = new Boolean(false);
console.log('b: ' + b);

if(b){
    console.log('A Boolean Object that value is false is true.');
} else {
    console.log('A Boolean Object that value is false is false.');
}


// End: if에 들어가는 해당 값에 대한 true, false

 

 

테스트.

https://jsfiddle.net/ogvm7168/