NomadCoders/momentum-chromeApp(vanillaJS)

[#02.4] Boolean

Heoky 2022. 6. 4. 17:14

Boolean은 true와 false를 말한다. 즉, 참이냐 거짓이냐를 뜻한다.

아래의 코드를 보면 null과 undefined가 나온다. JS에서 null은 object를 반환한다.
null은 의도적으로 값이 없다를 설정한 것이고, undefined는 아무것도 하지 않은 상태를 의미한다.
의도적으로 값을 비어있는 상태로 하는 것이 아니라면, undefined 사용은 부적절하다.

JS에서 일치연산자(==)와 동등연산자(===)가 있는데, 동등연산자는 자료형까지 엄격하게 비교해준다.
때문에 동등연산자를 활용하는 것이 좋다.
null과 undefined를 비교하기 위해선 동등연산자(===)를 통해 엄격하게 비교해주는 것이 좋다.

console.log('Boolean'); // Boolean

const test = null;
let test02;

console.log('null:', test); // null
console.log('test02:', test02); // undefined

console.log('===:', test === test02); // object
console.log('==:', test == test02); // true
console.log('!==:', test !== test02); // true

console.log('type of null:', typeof test); // object
console.log('type of undefined:', typeof test02); // undefined

// null과 undefined의 값을 비교할 때는 동등연산자(===)를 통해 엄격하게 자료형까지 비교하는 것이 종다.
// type of null은 object를 반환하기 때문이다.

 

'NomadCoders > momentum-chromeApp(vanillaJS)' 카테고리의 다른 글

vanillaJS의 키워드 3가지(코드)  (0) 2022.06.07
vanillaJS MDN 활용하기  (0) 2022.06.07
[#02.3] Variables (const and let)  (0) 2022.06.04
[#02] Your First JS Project  (0) 2022.06.04
[#01] Why JS?  (0) 2022.06.03