Javascript 변수의 형변환

feelslikemmmm·2020년 7월 24일
0

javascript

목록 보기
10/37
post-thumbnail

변수의 형변환

<script>
    let one = 1;
    console.log(`one + one : ${one + one}`); //2
    console.log(`String(one) + String(one) : ${String(one) + String(one)}`); // 11

    let two = '2';
    console.log(`two + two : ${two + two}`); //22
    console.log(`Number(two) + Number(two) : ${Number(two) + Number(two)}`); // 4

    let three = two + two;
    console.log(`typeof(three): ${typeof(three)}`); //String

    //0이외의 숫자들은 true고 문자열도 스페이스가 들어가면 true이다.
    console.log(`Boolean(1): ${Boolean(1)}`); //true
    console.log(`Boolean(-1): ${Boolean(-1)}`); //true
    console.log(`Boolean(0): ${Boolean(0)}`); //false
    console.log(`Boolean(' '): ${Boolean(' ')}`); //true
    console.log(`Boolean(''): ${Boolean('')}`); //false
    console.log(`Boolean([]): ${Boolean([])}`); //true
    console.log(`Boolean([0]): ${Boolean([0])}`); //true
    console.log(`Boolean('0'): ${Boolean('0')}`); //숫자0은 false이지만 문자열 0은 true이다.
</script>
profile
꾸준함을 잃지 말자는 모토를 가지고 개발하고 있습니다 :)

0개의 댓글